Display TPT in EF code First 4.1 w / Various primary keys

I am trying to match the TPT inheritance hierarchy in the old database (I cannot change the column names). All examples have primary keys of parent and child tables with the same name. Unfortunately, mine does not behave like that.

As a simplified example:

Vehicle ---------------- VehicleId Make Model ---------------- Car ---------------- CarId SomeOtherField ---------------- 

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

+6
source share
3 answers

Here's an expression about this problem from the EF team:

Unfortunately, this is not possible at this stage. I understand this is not a great answer, but one option would be to create a view that renames the PK / FK column and then map it.

This is from February 2011. Thus, this was due to an earlier version of the CTP EF Code-First. But I believe that this scenario is still not supported in EF 4.1.

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

+3
source

I got this to work by doing the following:

  • Remove all navigation properties between the base and inherited objects.
  • Remove the foreign key from the inherited objects, and also remove the property mapping for the foreign key in the inherited objects.
  • Delete table mapping in inherited objects.
  • Add the generated database parameter for the primary key of the inherited objects (if you use the identifier on the PC).
  • Add a base type map to the derived type, and (an important bit) on the map explicitly display each property that does not appear in the base class / table. On the map, also map the derived type to the derived type table.

That should pretty much do it. A link to an example solution in EF ↔ RIA ↔ Silverlight, which also shows a workaround for a property with the same name in the base and derived types (the solution is essentially simple for renaming the property to any base type or derived types).

http://dotnetdavis.com/upload/content/source.zip

+1
source

I found the answer for this article here: http://entityframework.codeplex.com/workitem/2087 . For your case you need:

  • In C # define a Vehicle object (VehicleId, Make, Model)
  • In C #, define the Car object (SomeOtherField) obtained from the Vehicle // CarId column will display in VehicleId, as shown below.
  • In the 'protected override void OnModelCreating (DbModelBuilder modelBuilder) do the following:

    modelBuilder.Entity () .ToTable ("Car") .Property (t => t.VehicleId) .HasColumnName ("CarId");

0
source

Source: https://habr.com/ru/post/892688/


All Articles