我有一组非常简单的数据库表,例如:
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 使用每个类型的表?
如果我从 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 文章!