Entity Framework v6 中的 TPC
TPC in Entity Framework v6
我正在尝试在 Entity Framework 中做一件非常简单的事情。
我的产品有零个或多个参数,这些参数将映射到它们自己的 table。但是,我无法让它工作。我一直在努力获得正确的映射,然后使用迁移来查看数据库应该是什么样子。我知道这在 NHibernate 中非常简单,但我被迫违背自己的意愿使用 Entity Framework v6.
背景
这些是我的实体:
namespace Entities
{
public class EntityState
{
public int Id { get; set; }
}
public class ProductState : EntityState
{
public virtual ICollection<ProductParameterState> Parameters { get; set; }
}
public abstract class ProductParameterState : EntityState
{
}
public class ColorParameterState : ProductParameterState
{
public virtual string Color { get; set; }
}
public class SizeParameterState : ProductParameterState
{
public virtual int Size { get; set; }
}
}
我想将其存储在以下架构中:
如何操作?
我的尝试
Table-每-class
我尝试使用 TPC 进行映射:
namespace Mappings
{
public class ProductMap : EntityTypeConfiguration<ProductState>
{
public ProductMap()
{
HasKey(x => x.Id);
Property(x => x.Name);
HasMany(x => x.Parameters);
}
}
public class ColorParameterMap : EntityTypeConfiguration<ColorParameterState>
{
public ColorParameterMap()
{
HasKey(x => x.Id);
Property(x => x.Color);
Map(x =>
{
x.ToTable("ColorParameters");
x.MapInheritedProperties();
});
}
}
public class SizeParameterMap : EntityTypeConfiguration<SizeParameterState>
{
public SizeParameterMap()
{
HasKey(x => x.Id);
Property(x => x.Size);
Map(x =>
{
x.ToTable("SizeParameters");
x.MapInheritedProperties();
});
}
}
}
但这给出了错误 The association 'ProductState_Parameters' between entity types 'ProductState' and 'ProductParameterState' is invalid. In a TPC hierarchy independent associations are only allowed on the most derived types.
。
不要使用继承策略
所以我试图删除 MapInheritedProperties
,但它想创建一个额外的、不需要的 table:
CreateTable(
"dbo.ProductParameterStates",
c => new
{
Id = c.Int(nullable: false, identity: true),
ProductState_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.ProductStates", t => t.ProductState_Id)
.Index(t => t.ProductState_Id);
我不想要这个。我可以通过删除 Product
中的 Parameters
属性 来摆脱这个,但是我无法使用 [=17] 的 Parameter
s =].
我是要求太多了还是有可能?
您可以使用 TPC,但关系必须是双向的,并且定义了明确的 FK(我猜这与错误消息中提到的 "independent association" 相反)。
向您的基本实体添加反向导航 属性 和 FK 属性:
public abstract class ProductParameterState : EntityState
{
public int ProductId { get; set; }
public ProductState Product { get; set; }
}
并使用与您第一次尝试时相同的实体配置,除了 ProductMap
您可以删除以下
HasMany(x => x.Parameters);
或改为
HasMany(e => e.Parameters)
.WithRequired(e => e.Product)
.HasForeignKey(e => e.ProductId);
我正在尝试在 Entity Framework 中做一件非常简单的事情。
我的产品有零个或多个参数,这些参数将映射到它们自己的 table。但是,我无法让它工作。我一直在努力获得正确的映射,然后使用迁移来查看数据库应该是什么样子。我知道这在 NHibernate 中非常简单,但我被迫违背自己的意愿使用 Entity Framework v6.
背景
这些是我的实体:
namespace Entities
{
public class EntityState
{
public int Id { get; set; }
}
public class ProductState : EntityState
{
public virtual ICollection<ProductParameterState> Parameters { get; set; }
}
public abstract class ProductParameterState : EntityState
{
}
public class ColorParameterState : ProductParameterState
{
public virtual string Color { get; set; }
}
public class SizeParameterState : ProductParameterState
{
public virtual int Size { get; set; }
}
}
我想将其存储在以下架构中:
如何操作?
我的尝试
Table-每-class
我尝试使用 TPC 进行映射:
namespace Mappings
{
public class ProductMap : EntityTypeConfiguration<ProductState>
{
public ProductMap()
{
HasKey(x => x.Id);
Property(x => x.Name);
HasMany(x => x.Parameters);
}
}
public class ColorParameterMap : EntityTypeConfiguration<ColorParameterState>
{
public ColorParameterMap()
{
HasKey(x => x.Id);
Property(x => x.Color);
Map(x =>
{
x.ToTable("ColorParameters");
x.MapInheritedProperties();
});
}
}
public class SizeParameterMap : EntityTypeConfiguration<SizeParameterState>
{
public SizeParameterMap()
{
HasKey(x => x.Id);
Property(x => x.Size);
Map(x =>
{
x.ToTable("SizeParameters");
x.MapInheritedProperties();
});
}
}
}
但这给出了错误 The association 'ProductState_Parameters' between entity types 'ProductState' and 'ProductParameterState' is invalid. In a TPC hierarchy independent associations are only allowed on the most derived types.
。
不要使用继承策略
所以我试图删除 MapInheritedProperties
,但它想创建一个额外的、不需要的 table:
CreateTable(
"dbo.ProductParameterStates",
c => new
{
Id = c.Int(nullable: false, identity: true),
ProductState_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.ProductStates", t => t.ProductState_Id)
.Index(t => t.ProductState_Id);
我不想要这个。我可以通过删除 Product
中的 Parameters
属性 来摆脱这个,但是我无法使用 [=17] 的 Parameter
s =].
我是要求太多了还是有可能?
您可以使用 TPC,但关系必须是双向的,并且定义了明确的 FK(我猜这与错误消息中提到的 "independent association" 相反)。
向您的基本实体添加反向导航 属性 和 FK 属性:
public abstract class ProductParameterState : EntityState
{
public int ProductId { get; set; }
public ProductState Product { get; set; }
}
并使用与您第一次尝试时相同的实体配置,除了 ProductMap
您可以删除以下
HasMany(x => x.Parameters);
或改为
HasMany(e => e.Parameters)
.WithRequired(e => e.Product)
.HasForeignKey(e => e.ProductId);