NotSupportedException:无法按定义映射类型A.混凝土表(TPC)EF6



我有这样的模型:

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

和配置类:

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

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

但是得到:

System.NotSupportedException:类型"ItemCat"无法按定义映射,因为它映射的是从使用实体拆分或其他形式继承的类型继承的属性。要么选择不同的继承映射策略以不映射继承的属性,要么更改层次结构中的所有类型以映射继承的特性并不使用拆分

更新:我也阅读了这个

找到答案。只需删除ItemCatConfig类中的Map即可。

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

在TPC中,抽象类不在数据库中实现。ItemCat继承自抽象类,不需要显式映射配置。

最新更新