0

私は次のようなモデルを持っています:

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' は、エンティティ分割または別の形式の継承を使用する型から継承されたプロパティをマップするため、定義どおりにマップできません。継承されたプロパティをマップしないように別の継承マッピング戦略を選択するか、階層内のすべてのタイプを変更して継承されたプロパティをマップし、分割を使用しないようにします。

更新:私もこれを読みました

4

1 に答える 1

0

答えを見つけてください。ItemCatConfig クラスのMapを削除するだけです。

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

TPC では、抽象クラスは db に実装されていません。ItemCat は抽象クラスから継承され、構成を明示的にマップする必要はありません。

于 2014-04-10T19:01:28.223 に答える