[英]Multiple levels of inheritance with Entity Framework using TPH
I am using Entity Framework to create a Table Per Heirachy data model in a new project over an existing legacy database. 我正在使用Entity Framework在现有遗留数据库的新项目中创建Table Per Heirachy数据模型。 This means that we don't own the database and therefore I need a solution that doesn't involve adding columns to the OrderDiscount table.
这意味着我们不拥有数据库,因此我需要一个不涉及向OrderDiscount表添加列的解决方案。
I have the following structure in my data models: 我的数据模型中有以下结构:
┌────── (abstract) OrderDiscountBase ──────┐
│ │
│ │
SaleOrderDiscount ┌─── (abstract) SellerOrderDiscountBase ───┐
│ │
│ │
FreeShippingOrderDiscount PercentageValueOrderDiscount
There is a pre-existing discriminator column to distinguish between the concrete types. 有一个预先存在的鉴别器列来区分具体类型。 It's called
DiscountType
and can hold the values PercentageOffPromoDiscount
, FreeShippingPromoDiscount
or SaleDiscount
. 它被称为
DiscountType
,可以保存值PercentageOffPromoDiscount
, FreeShippingPromoDiscount
或SaleDiscount
。
I have the following mapping in my project, which is--unfortunately--not working. 我的项目中有以下映射,不幸的是,它没有工作。
private void ConfigureOrderDiscountEntity(EntityTypeConfiguration<OrderDiscountBase> entity)
{
entity
.HasKey(orderDiscount => orderDiscount.Id)
.Map<SaleOrderDiscount>(
configuration => configuration.Requires("DiscountType").HasValue(OrderDiscountTypes.Sale))
.Map<PercentageValueOrderDiscount>(
configuration => configuration.Requires("DiscountType").HasValue(OrderDiscountTypes.PercentageOff))
.Map<FreeShippingOrderDiscount>(
configuration => configuration.Requires("DiscountType").HasValue(OrderDiscountTypes.FreeShipping))
.ToTable(tableName: "OrderDiscount", schemaName: "Orders")
;
}
The exception I see is: 我看到的例外是:
EntityTypes SellerOrderDiscountBase, SaleOrderDiscount, FreeShippingOrderDiscount, PercentageValueOrderDiscount are being mapped to the same rows in table OrderDiscountBase. EntityTypes SellerOrderDiscountBase,SaleOrderDiscount,FreeShippingOrderDiscount,PercentageValueOrderDiscount被映射到表OrderDiscountBase中的相同行。 Mapping conditions can be used to distinguish the rows that these types are mapped to.
映射条件可用于区分这些类型映射到的行。
I've seen people solve this problem in older versions of Entity Framework by adding a second discriminator column but, as I said, I can't make changes to the database. 我已经看到人们通过添加第二个鉴别器列来解决旧版本的Entity Framework中的这个问题,但正如我所说,我无法对数据库进行更改。
What I'm asking EF to do doesn't sound difficult, so I assume I have just configured my entities incorrectly. 我要求EF做的并不难,所以我假设我刚刚错误地配置了我的实体。
Definitely works for me in EF 6.x: 绝对适用于EF 6.x:
public abstract class TPHBase
{
public int ID { get; set; }
public string CommonProperty { get; set; }
}
public class TPHChild1 : TPHBase
{
public string Child1Property { get; set; }
}
public abstract class TPHChild2 : TPHBase
{
public int? Child2Property { get; set; }
}
public class TPHChild3 : TPHChild2
{
public int? Child3Property { get; set; }
}
public class TPHChild4 : TPHChild2
{
public int? Child4Property { get; set; }
}
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
modelBuilder.Entity<TPHBase>()
.ToTable( "TPHBase" )
.Map<TPHChild1>( m => m.Requires( "Dyskryminator" ).HasValue( "c1" ) )
.Map<TPHChild3>( m => m.Requires( "Dyskryminator" ).HasValue( "c3" ) )
.Map<TPHChild4>( m => m.Requires( "Dyskryminator" ).HasValue( "c4" ) );
...
Entities are sucesfully persisted and then retrieved from the database. 实体被强制保留,然后从数据库中检索。
I suspect there is something more than you have posted. 我怀疑还有比你发布的更多的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.