简体   繁体   中英

EF4 and multiple abstract levels

I need to use inheritance with EF4 and the TPH model created from DB.
I created a new projet to test simples classes. There is my class model:

在此处输入图片说明

There is my table in 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.

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. 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. 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) :
There are all of mappings details : 在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

What's wrong ?

Thanks

EF4 doesn't handle these well. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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