NotSupportedException:类型A无法映射为定义。混凝土表(TPC)EF6 [英] NotSupportedException: The type A cannot be mapped as definede. Table per Concrete (TPC) EF6

查看:270
本文介绍了NotSupportedException:类型A无法映射为定义。混凝土表(TPC)EF6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public abstract class Entity 
{
public int Id {get ;组;


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

public int? ParentId {get;组; }

public string Name {get;组; }

[ForeignKey(ParentId)]
public ICollection< Tree>孩子{get;组;

}

public abstract class Cat:Tree
{

public string ImageUrl {get;组; }

public string描述{get;组; }

public int OrderId {get;组;

}

public class ItemCat:Cat
{
...
public virtual ICollection< Item>物品{get;组;
}

和配置类:

  public class CatConfig:EntityTypeConfiguration< Cat> 
{
public CatConfig()
{
//属性
属性(rs => rs.Name).IsUnicode();
属性(rs => rs.ImageUrl).IsUnicode();
属性(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;组;
}
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Configurations.Add(new ItemCatConfig());

base.OnModelCreating(mb);
}

但获取:



< blockquote>

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


更新:我也阅读

解决方案

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

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

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


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

解决方案

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.

这篇关于NotSupportedException:类型A无法映射为定义。混凝土表(TPC)EF6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆