簡體   English   中英

實體框架TPC外鍵關系

[英]Entity Framework TPC Foreign Key relationships

所以我實現了Table Per具體的Class來處理繼承層次結構,但是我遇到了導航屬性的問題。

我的模型結構如下:

我有一個抽象的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
}

這里的評論導航屬性不起作用,因為每個派生(混凝土)類都有自己的一組ID。 在我看來,每個表中的EntityType列都可以作為某種Discriminator,但我不知道如何告訴EF這樣使用它。

任何幫助將不勝感激!

TPC工作的解決方案是派生類中的Ids不僅在其表中而且在兩個表中都是唯一的。

您可以使用數據庫解決方案,例如具有不同初始種子的自動增量主鍵或GUID鍵,如SQL Server中的那些。

另一種方法是在應用程序代碼中生成唯一的GUID鍵。 您可以看到如何為以下實體建模的相同示例代碼:

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; }
    }

}

要創建一些實體,請使用以下代碼:

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();
}

TPC的一些好資源將在這里:

[EF代碼優先繼承:第3部分 - 每種混凝土類型(TPC)表]

[實體框架 - 每個具體表]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM