NotSupportedException: The type A cannot be mapped as definede. Table per Concrete (TPC) EF6

无人久伴 提交于 2019-12-12 05:33:18

问题


I have model like:

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

public abstract  class Tree : Entity
    {
        public Tree() { Childs = new List<Tree>(); }

        public int? ParentId { get; set; }

        public string Name { get; set; }

        [ForeignKey("ParentId")]
        public ICollection<Tree> Childs { get; set; }

    }

 public  abstract class Cat : Tree
    {

        public string ImageUrl { get; set; }

        public string Description { get; set; }

        public int OrderId { get; set; }

    }

  public class ItemCat : Cat
        {
            ...
            public virtual ICollection<Item> Items { get; set; }
        }

and config classes:

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

 public class ItemCatConfig :EntityTypeConfiguration<ItemCat>
    {
        public ItemCatConfig()
        {

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

and DbContext:

public class Db :  IdentityDbContext<MehaUser>
    {
        public Db():base("Db")
        {
        }

        public DbSet<ItemCat> ItemCats { get; set; }
    }
 protected override void OnModelCreating(DbModelBuilder mb)
        {
            mb.Configurations.Add(new ItemCatConfig());

            base.OnModelCreating(mb);
        }

but get:

System.NotSupportedException: The type 'ItemCat' cannot be mapped as defined because it maps inherited properties from types that use entity splitting or another form of inheritance. Either choose a different inheritance mapping strategy so as to not map inherited properties, or change all types in the hierarchy to map inherited properties and to not use splitting

Update: I also Read this


回答1:


Find the answer. just remove Map in ItemCatConfig Class.

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

In TPC abstract classes does not implement in db. ItemCat inherit from abstract classes and it doesn't need to Map configuration explicitly.



来源:https://stackoverflow.com/questions/22983722/notsupportedexception-the-type-a-cannot-be-mapped-as-definede-table-per-concre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!