簡體   English   中英

地圖TPT戰略實體框架代碼拳頭

[英]Map TPT Strategy Entity Framework Code fist

我首先在EF代碼上工作,我喜歡抽象! 所以想擁有像這樣的ItemCat實體:

public abstract class EntityBase
    {
        public int Id { get; set; }
    }

public abstract  class TreeBase : EntityBase
    {
        public int? ParentId { get; set; }

        public string Name { get; set; }

        public virtual TreeBase Parent { get; set; }
        public virtual ICollection<TreeBase> Children { get; set; }

    }

public  abstract class CatBase : TreeBase
    {

        public string ImageUrl { get; set; }

        public string Description { get; set; }


        public int OrderId { get; set; }

    }

public class ItemCat : CatBase
    {
        public stringName { get; set; }

// other fields...
        public virtual ICollection<Item> Items { get; set; }

    }

我的地圖startgey是每種類型的表.TPT

ItemCat的所有基類都由abstract關鍵字解析。 但是在遷移中我得到了Db中的TreeBases表,為什么呢? 我很奇怪,因為它是抽象的。 我的映射是否需要明確定義任何配置? 即時通訊使用EF 6

編輯還為遷移中的EF創建TreeBase表的Discriminator列,當我插入Record時,它具有ItemCat值。

編輯

protected override void OnModelCreating(DbModelBuilder mb)
        {
            //Item

            mb.Configurations.Add(new TreeBaseConfig());
            mb.Configurations.Add(new CatConfig());

        }

 public class TreeBaseConfig:EntityTypeConfiguration<TreeBase>
    {
        public TreeBaseConfig()
        {
            HasMany(rs => rs.Children).WithOptional(rs => rs.Parent).HasForeignKey(rs => rs.ParentId).WillCascadeOnDelete(false);
        }
    }

public  class CatConfig : EntityTypeConfiguration<CatBase>
    {
        public CatConfig()
        {
            //properties
            Property(rs => rs.Name).IsUnicode();
            Property(rs => rs.ImageUrl).IsUnicode();
            Property(rs => rs.Description).IsUnicode();
        }
    }

編輯

我添加了ItemCatConfig類:

public ItemCatConfig()
    {

        //map
       Map(m =>  { m.ToTable("ItemCats"); m.MapInheritedProperties(); });
    }

但得到:

“ItemCat”類型無法按定義映射,因為它映射了使用實體拆分或其他形式的繼承的類型的繼承屬性。 選擇不同的繼承映射策略,以便不映射繼承的屬性,或更改層次結構中的所有類型以映射繼承的屬性,並且不使用拆分。

在執行ToTable()MapInheritedProperties()時,如果不執行任何ToTable()映射和TPC(每個混凝土類型的表ToTable() ,則使用了TPH(每個層次表MapInheritedProperties() 如果要使用TPT(Table Per Type),只執行ToTable()映射並關閉對MapInheritedProperties()的調用,如下所示:

ToTable("ItemCats");

暫無
暫無

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

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