Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping TPT in EF Code First 4.1 w/ Different Primary Keys

I am trying to map a TPT inheritance hierarchy on a legacy database (I can't change column names). All of the examples have the primary keys of the parent and children tables with the same name. Unfortunately, mine doesn't behave this way.

As a simplified example:

Vehicle
----------------
VehicleId
Make
Model
----------------

Car
----------------
CarId        
SomeOtherField
----------------

CarId and VehicleId are actually the same id and are the values that should be used to associate the tables. Is there any support for creating this as a TPT relationship in Code First?

like image 636
Keith Rousseau Avatar asked Jul 13 '11 15:07

Keith Rousseau


1 Answers

Here is a statement about this issue from the EF team:

Unfortunately this isn't possible at this stage. I realize this isn't a great answer but one option would be to create a view that renames the PK/FK column and then map to that.

It's from February 2011. So it was related to an earlier CTP version of EF Code-First. But I believe that this scenario is still not supported in EF 4.1.

Also a similar question here with not satisfying result regarding EF 4.1 Code-First: How can I use TPT inheritance models when primary keys have different names?

like image 143
Slauma Avatar answered Oct 17 '22 05:10

Slauma