【问题标题】:NotSupportedException: The type A cannot be mapped as definede. Table per Concrete (TPC) EF6NotSupportedException:类型 A 无法按定义映射。每混凝土表 (TPC) EF6
【发布时间】:2014-05-23 21:43:10
【问题描述】:

我有这样的模型:

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

更新:我也读过this

【问题讨论】:

    标签: c# entity-framework code-first table-per-class


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多