简体   繁体   English

实体框架TPC外键关系

[英]Entity Framework TPC Foreign Key relationships

So I've implement Table Per concrete Class to deal with an inheritance hierarchy, but I'm having trouble with Navigation properties. 所以我实现了Table Per具体的Class来处理继承层次结构,但是我遇到了导航属性的问题。

My Model is structure as follows: 我的模型结构如下:

I have an abstract BaseEntity class, with Multiple derived classes, so: 我有一个抽象的BaseEntity类,有多个派生类,所以:

public abstract class BaseEntity
{
    public virtual ICollection<Comment> Comments { get; set; }
    public EntitytType EntitytType { get; set; }
}

public class FirstDerivedEntity : BaseEntity
{
    //EntitytType == First;
}

public class SecondDerivedEntity : BaseEntity
{
    //EntitytType == Second;
}

public class Comment
{
    public long BaseEntityId { get; set; }
    public EntitytType BaseEntityType { get; set; }
}

public enum EntitytType
{
    First,
    Second
}

The Comments navigation property here doesn't work because each derived(Concrete) class has it's own set of Ids. 这里的评论导航属性不起作用,因为每个派生(混凝土)类都有自己的一组ID。 In my mind the EntityType column in each Table would serve as some sort of Discriminator, but I don't know how to tell EF to use it as such. 在我看来,每个表中的EntityType列都可以作为某种Discriminator,但我不知道如何告诉EF这样使用它。

Any help would be greatly appreciated! 任何帮助将不胜感激!

The solution in order for TPC to work would be the Ids in the derived classes to be unique not only in their table but in both tables. TPC工作的解决方案是派生类中的Ids不仅在其表中而且在两个表中都是唯一的。

You can use a database solution like an auto increment primary key with different initial seeds or GUID keys like the ones in SQL server. 您可以使用数据库解决方案,例如具有不同初始种子的自动增量主键或GUID键,如SQL Server中的那些。

Another approach would be to generate unique GUID keys in you application code. 另一种方法是在应用程序代码中生成唯一的GUID键。 You can see same sample code of how to model the entities below : 您可以看到如何为以下实体建模的相同示例代码:

namespace tpc_so
{
    public class tpc_soContext : DbContext
    {
        public tpc_soContext()
        {
        }
        public DbSet<BaseEntity> BaseEntities { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BaseEntity>().HasKey(b => b.BaseEntityId);
            modelBuilder.Entity<BaseEntity>()
           .Property(b => b.BaseEntityId)
           .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            modelBuilder.Entity<FirstDerivedEntity>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("FirstDerivedEntities");
            });

            modelBuilder.Entity<SecondDerivedEntity>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("SecondDerivedEntities");
            });


            modelBuilder.Entity<Comment>().ToTable("Comments");

            base.OnModelCreating(modelBuilder);
        }

    }
    public abstract class BaseEntity
    {
        public Guid BaseEntityId { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }

    public class FirstDerivedEntity : BaseEntity{}
    public class SecondDerivedEntity : BaseEntity{ }

    public class Comment
    {
        public long CommentId { get; set; }
        public Guid BaseEntityId { get; set; }
        public string Text { get; set; }
    }

}

And to create some entities use the code below : 要创建一些实体,请使用以下代码:

using (var ctx = new tpc_soContext())
{
    ctx.BaseEntities.Add(new FirstDerivedEntity()
    {
        BaseEntityId = Guid.NewGuid(),
        Comments = new List<Comment>() { new Comment() { Text = "First Derived Comment" } }
    });

    ctx.BaseEntities.Add(new SecondDerivedEntity()
    {
        BaseEntityId = Guid.NewGuid(),
        Comments = new List<Comment>() { new Comment() { Text = "Second-Derived Comment" } }
    });

    ctx.SaveChanges();
}

Some good sources for TPC would be here : TPC的一些好资源将在这里:

[Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC)] [EF代码优先继承:第3部分 - 每种混凝土类型(TPC)表]

[Entity Framework - Table Per Concrete] [实体框架 - 每个具体表]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM