使用 EF 4.1 Fluent Code First 进行按类型继承
我有一组非常简单的数据库表,例如:
Vehicle
Id
RegNo
Car
Id (FK of Vehicle.Id)
OtherStuff
Bike
Id (FK of Vehicle.Id)
MoreStuff
我的类模型如您所期望的那样:Vehicle 是一个抽象类,然后 Car 和 Bike 是它的子类。
我已按如下方式设置 EF4.1 Code First 配置:
class VehicleConfiguration : EntityTypeConfiguration<Vehicle> {
public VehicleConfiguration() {
ToTable("Vehicles");
Property(x => x.Id);
Property(x => x.RegNo);
HasKey(x => x.Id);
}
}
class CarConfiguration : EntityTypeConfiguration<Car> {
public CarConfiguration() {
ToTable("Cars");
Property(x => x.OtherStuff);
}
}
class BikeConfiguration : EntityTypeConfiguration<Bike> {
public BikeConfiguration() {
ToTable("Bikes");
Property(x => x.MoreStuff);
}
}
但是,当 EF 尝试构建其模型配置时,我遇到了许多奇怪的异常。
目前它正在抛出这个:
System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'Discriminator'.
它从哪里获取该列名称?它不在我的任何代码或数据库本身中。一定是某种惯例正在接管控制权。如何指示 EF 使用 table-per-type?
如果我从我的 Vehicle 类中删除“abstract”关键字(我在某处进行了健全性测试),那么我会得到一个不同的异常,如下所示:
(35,10) : error 3032: Problem in mapping fragments starting at lines 30, 35:EntityTypes AcmeCorp.Car, AcmeCorp.Bike are being mapped to the same rows in table Vehicles. Mapping conditions can be used to distinguish the rows that these types are mapped to.
我显然做了一些非常错误的事情,但是什么呢?我已遵循 MSDN 文档以及我能找到的所有其他 TPT + EF4.1 文章!
I have a pretty straight forward set of database tables, like:
Vehicle
Id
RegNo
Car
Id (FK of Vehicle.Id)
OtherStuff
Bike
Id (FK of Vehicle.Id)
MoreStuff
My class model is as you'd expect: with Vehicle being an abstract class, and then Car and Bike being subclasses of it.
I have setup my EF4.1 Code First configuration as follows:
class VehicleConfiguration : EntityTypeConfiguration<Vehicle> {
public VehicleConfiguration() {
ToTable("Vehicles");
Property(x => x.Id);
Property(x => x.RegNo);
HasKey(x => x.Id);
}
}
class CarConfiguration : EntityTypeConfiguration<Car> {
public CarConfiguration() {
ToTable("Cars");
Property(x => x.OtherStuff);
}
}
class BikeConfiguration : EntityTypeConfiguration<Bike> {
public BikeConfiguration() {
ToTable("Bikes");
Property(x => x.MoreStuff);
}
}
However I am getting numerous strange exceptions when EF tried to build its model configuration.
Currently it is throwing out this:
System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'Discriminator'.
Where is it getting that column name from? It's not in any of my code or the database itself. It must be some convention that's taking over control. How do I instruct EF to use table-per-type?
If I remove the "abstract" keyword from my Vehicle class (which I did as a sanity test somewhere along the line) then I get a different exception like the following:
(35,10) : error 3032: Problem in mapping fragments starting at lines 30, 35:EntityTypes AcmeCorp.Car, AcmeCorp.Bike are being mapped to the same rows in table Vehicles. Mapping conditions can be used to distinguish the rows that these types are mapped to.
I'm obviously doing something terribly wrong, but what? I've followed the MSDN docs and all the other TPT + EF4.1 articles I can find!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您读过下面的文章了吗?
这是一篇由 3 部分组成的文章,涵盖以下方法
每个层次结构表 (TPH): 通过以下方式启用多态性非规范化
SQL 模式,并利用保存类型的类型鉴别器列
信息。
每个类型的表(TPT): 表示“is a”(继承)
关系作为“有”(外键)关系。
每个混凝土类别 (TPC) 的表: 丢弃多态性和
继承关系完全来自 SQL 架构。
Have you read the following article?
It is a 3 part article covering the following approaches
Table per Hierarchy (TPH): Enable polymorphism by denormalizing the
SQL schema, and utilize a type discriminator column that holds type
information.
Table per Type (TPT): Represent "is a" (inheritance)
relationships as "has a" (foreign key) relationships.
Table per Concrete class (TPC): Discard polymorphism and
inheritance relationships completely from the SQL schema.
当我遇到这个问题时,我发现我有一个没有映射的子类。在此示例中,一些可能的原因是:
Bus
。这没有映射。Car
)未映射。在这种情况下,请确保每个子类都已映射:
ToTable
方法调用。或者,如果首先不应映射子类,请确保使用其中一个
Ignore
方法调用来忽略它。When I had this problem, I discovered that I had a subclass that was not mapped. In this example, some possible causes of this are:
Bus
. This is not mapped.Car
, is not mapped.In this case, ensure that every subclass is mapped:
ToTable
method call.OnModelCreating
.Alternatively, if the subclass shouldn't be mapped in the first place, ensure that it is ignored by using one of the
Ignore
method calls.