我有这样的模型:
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”类型无法按定义映射,因为它映射了从使用实体拆分或其他形式继承的类型继承的属性。选择不同的继承映射策略以便不映射继承的属性,或者更改层次结构中的所有类型以映射继承的属性并且不使用拆分
更新:我也读过这个