简体   繁体   English

EF4和多个抽象级别

[英]EF4 and multiple abstract levels

I need to use inheritance with EF4 and the TPH model created from DB. 我需要对EF4和从数据库创建的TPH模型使用继承。
I created a new projet to test simples classes. 我创建了一个新的projet来测试简单类。 There is my class model: 有我的班级模型:

在此处输入图片说明

There is my table in SQL SERVER 2008 : 在SQL SERVER 2008中有我的表:

VEHICLE 
  ID : int PK
  Owner : varchar(50)
  Consumption : float
  FirstCirculationDate : date
  Type : varchar(50)
  Discriminator : varchar(10)

I added a condition in my EDMX on the Discriminator field to differentiate the Scooter, Car, Motorbike and Bike entities. 我在EDMX的Discriminator字段上添加了一个条件,以区分Scooter,Car,Motorbike和Bike实体。

MotorizedVehicle and Vehicle are Abstract. 机动车辆和车辆是抽象的。

But when I compile, this error appears : 但是当我编译时,出现此错误:

Error 3032: Problem in mapping fragments starting at lines 78, 85:EntityTypes EF4InheritanceModel.Scooter, EF4InheritanceModel.Motorbike, EF4InheritanceModel.Car, EF4InheritanceModel.Bike are being mapped to the same rows in table Vehicle. 错误3032:映射片段的问题从第78行和第85行开始:EntityTypes EF4InheritanceModel.Scooter,EF4InheritanceModel.Motorbike,EF4InheritanceModel.Car,EF4InheritanceModel.Bike被映射到表Vehicle中的相同行。 Mapping conditions can be used to distinguish the rows that these types are mapped to. 映射条件可用于区分这些类型映射到的行。

Edit : 编辑:
To Ladislav : 致拉迪斯拉夫:
I try it and error change to become it for all of my entities : 我尝试了一下,并将错误更改为所有实体的错误:

Error 3034: Problem in mapping fragments starting at lines 72, 86:An entity is mapped to > different rows within the same table. 错误3034:映射片段的问题从第72、86行开始:实体被映射到同一表中的>不同行。 Ensure these two mapping fragments do not map two > groups of entities with overlapping keys to two distinct groups of rows. 确保这两个映射片段不会将键重叠的两个实体组映射到两个不同的行组。

To Henk (with Ladislay suggestion) : 致Henk(建议Ladislay):
There are all of mappings details : 有所有映射详细信息: 在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

What's wrong ? 怎么了 ?

Thanks 谢谢

EF4 doesn't handle these well. EF4不能很好地处理这些问题。 You can do it with a discriminator, but the classes need to be non-abstract. 您可以使用区分符来实现,但是这些类必须是非抽象的。 You'll have to use another method like code analysis or tests to ensure that the base class never gets instantiated by anything other than Entity Framework. 您必须使用另一种方法,例如代码分析或测试,以确保基类不会被除实体框架以外的任何实例所实例化。

Or, you could upgrade to EF6. 或者,您可以升级到EF6。 I'm not sure if it will support it better, but it definitely handles model hierarchies better, allowing for better database schemas that support the model better. 我不确定它是否会更好地支持它,但是它肯定会更好地处理模型层次结构,从而允许更好的数据库模式更好地支持模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM