Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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 : enter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description here

What's wrong ?

Thanks

like image 707
Cedric Avatar asked Oct 03 '12 07:10

Cedric


1 Answers

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.

like image 134
Jon Adams Avatar answered Oct 25 '22 10:10

Jon Adams