Code:

using System;
using System.Data.SqlClient;
using System.Linq;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;
using Xtensive.Orm.Upgrade;
using Domain = Xtensive.Orm.Domain;

[Serializable]
[HierarchyRoot]
[Recycled]
class RecycledEntity : Entity
{
    [Key]
    [Field(Nullable = false)]
    public Guid Id { get; private set; }
}

interface ITestInterface<T> : IEntity
{
}

[Serializable]
[HierarchyRoot]
[Recycled]
class RecycledWithInterfaceEntity : 
    Entity, ITestInterface<RecycledWithInterfaceEntity>
{
    [Key]
    [Field(Nullable = false)]
    public Guid Id { get; private set; }
}

class Program
{
    private const string ConnectionString = "Data Source=.; Initial Catalog=DO-Test;Connection Timeout=300;Integrated Security = true;Max pool size=2";

    static void Main(string[] args)
    {
        var dc = new DomainConfiguration("sqlserver", ConnectionString);
        dc.Types.Register(typeof(Program).Assembly);
        dc.UpgradeMode = DomainUpgradeMode.Recreate;

        using (var domain = Domain.Build(dc))
        {
            CheckTypeInModel(domain, typeof(RecycledWithInterfaceEntity));
            CheckTypeInModel(domain, typeof(RecycledEntity));
        }
    }

    static void CheckTypeInModel(Domain domain, Type type)
    {
        Console.WriteLine($"Type {type} found in model: " + 
            domain.Model.Types.TryGetValue(type, out var typeInfo));

        var expectedTableName = typeInfo?.MappingName ?? type.Name;
        Console.WriteLine($"Type mapping: {expectedTableName} ({type.Name})");

        object objectId;
        using (var conn = new SqlConnection(ConnectionString))
        using (var cmd = conn.CreateCommand())
        {
            conn.Open();

            cmd.CommandText = "SELECT OBJECT_ID(@table, N'U');";
            cmd.Parameters.AddWithValue("table", expectedTableName);
            objectId = cmd.ExecuteScalar();
        }

        Console.WriteLine($"Table {expectedTableName} exists: " + 
                          !DBNull.Value.Equals(objectId));
        Console.WriteLine();
    }
}

Output:

Type Sample.RecycledWithInterfaceEntity found in model: True
Type mapping: RecycledWithInterfaceEntity (RecycledWithInterfaceEntity)
Table RecycledWithInterfaceEntity exists: True

Type Sample.RecycledEntity found in model: False
Type mapping: RecycledEntity (RecycledEntity)
Table RecycledEntity exists: False

asked Nov 16 at 05:21

Platonov's gravatar image

Platonov
52

edited Nov 16 at 05:27

Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×4
×4
×2

Asked: Nov 16 at 05:21

Seen: 141 times

Last updated: Nov 16 at 05:27

powered by OSQA