映射TPT策略实体框架代码拳头 [英] Map TPT Strategy Entity Framework Code fist
问题描述
public abstract class EntityBase
{
public int Id {get ;组;
}
public abstract class TreeBase:EntityBase
{
public int? ParentId {get;组; }
public string Name {get;组; }
public virtual TreeBase Parent {get;组; }
public virtual ICollection< TreeBase>孩子{get;组;
}
public abstract class CatBase:TreeBase
{
public string ImageUrl {get;组; }
public string描述{get;组; }
public int OrderId {get;组;
}
public class ItemCat:CatBase
{
public stringName {get;组; }
//其他字段...
public virtual ICollection< Item>物品{get;组; }
}
我的地图startgey是每个类型的表.TPT
ItemCat的所有基类都由抽象关键字进行分解。但是在迁移我得到Db中的TreeBase表,真的是为什么?我很奇怪,因为它是抽象的。我的映射需要明确定义任何配置吗? im使用EF 6
编辑还可以在Migration中创建EF,为TreeBase表创建Discriminator列,当我插入Record时,它具有ItemCat值。
修改
protected override void OnModelCreating(DbModelBuilder mb)
{
// Item
mb.Configurations.Add(new TreeBaseConfig());
mb.Configurations.Add(new CatConfig());
}
public class TreeBaseConfig:EntityTypeConfiguration< TreeBase>
{
public TreeBaseConfig()
{
HasMany(rs => rs.Children).WithOptional(rs => rs.Parent).HasForeignKey(rs => rs.ParentId).WillCascadeOnDelete(假);
}
}
public class CatConfig:EntityTypeConfiguration< CatBase>
{
public CatConfig()
{
//属性
属性(rs => rs.Name).IsUnicode();
属性(rs => rs.ImageUrl).IsUnicode();
属性(rs => rs.Description).IsUnicode();
}
}
修改
我添加了ItemCatConfig类:
public ItemCatConfig()
{
// map
Map(m => {m.ToTable(ItemCats); m.MapInheritedProperties();});
}
但获取:
< blockquote>
类型'ItemCat'无法按照定义进行映射,因为它将
继承的属性映射到使用实体分割的类型或另一个
形式的继承。或者选择不同的继承映射
策略,以便不映射继承的属性,或者更改层次结构中的所有类型,以映射继承的属性并且不使用拆分。
当你不执行任何
ToTable()时,你已经使用过TPH(Table Per Hierarchy) code>映射和TPC(Table Per Concrete Type)当同时执行 ToTable()
和 MapInheritedProperties()
。如果要使用TPT(Table Per Type),请仅执行 ToTable()
映射,并将调用留在 MapInheritedProperties()
关闭,如下所示: ToTable(ItemCats);
Im working on EF Code first, I like abstraction! so want have ItemCat entity like this:
public abstract class EntityBase
{
public int Id { get; set; }
}
public abstract class TreeBase : EntityBase
{
public int? ParentId { get; set; }
public string Name { get; set; }
public virtual TreeBase Parent { get; set; }
public virtual ICollection<TreeBase> Children { get; set; }
}
public abstract class CatBase : TreeBase
{
public string ImageUrl { get; set; }
public string Description { get; set; }
public int OrderId { get; set; }
}
public class ItemCat : CatBase
{
public stringName { get; set; }
// other fields...
public virtual ICollection<Item> Items { get; set; }
}
My map startgey is table per type.TPT
All base classes for ItemCat are decoreated by abstract keyword. but in Migration i get TreeBases Table in Db, really why? i'm wonder because it's abstract. does my mapping need define any configuration explicitly? im using EF 6
Edit also EF in Migration create Discriminator column for TreeBase table and when i insert Record it has ItemCat value.
Edit
protected override void OnModelCreating(DbModelBuilder mb)
{
//Item
mb.Configurations.Add(new TreeBaseConfig());
mb.Configurations.Add(new CatConfig());
}
public class TreeBaseConfig:EntityTypeConfiguration<TreeBase>
{
public TreeBaseConfig()
{
HasMany(rs => rs.Children).WithOptional(rs => rs.Parent).HasForeignKey(rs => rs.ParentId).WillCascadeOnDelete(false);
}
}
public class CatConfig : EntityTypeConfiguration<CatBase>
{
public CatConfig()
{
//properties
Property(rs => rs.Name).IsUnicode();
Property(rs => rs.ImageUrl).IsUnicode();
Property(rs => rs.Description).IsUnicode();
}
}
Edit
I added ItemCatConfig Class:
public ItemCatConfig()
{
//map
Map(m => { m.ToTable("ItemCats"); m.MapInheritedProperties(); });
}
but get:
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.
解决方案 You have used TPH (Table Per Hierarchy) when you don't do any ToTable()
mapping, and TPC (Table Per Concrete Type) when doing both ToTable()
and MapInheritedProperties()
. If you want to use TPT (Table Per Type) do only the ToTable()
mapping and leave the call to MapInheritedProperties()
off, like so:
ToTable("ItemCats");
这篇关于映射TPT策略实体框架代码拳头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!