How to handle "secondary" keys in the Entity Framework

I appreciate the use of EF in relation to the existing schema - the problem is that I cannot figure out how to set up associations between tables, where the foreign key is NOT the primary key of the main table.

As an example, foo can have many bars , as defined (forgive pseudocode):

 table foo { int foo\_id pk, char(10) foo\_code, ... } table foobar { int bar\_id pk, char(10) bar\_foo\_code fk(foo.foo\_code), ... } 

What am I missing to create the foo_foobar association and therefore the bars navigation property for the foo object?

+6
mapping entity-framework foreign-keys
source share
1 answer

Linq for entities does not support foreign keys that do not point to the primary key of the table (see log message 3). Linq for entities will consider it as a normal field on a table. You cannot go to the object to which it is attached.

If you have an existing circuit, I would recommend using edm generator , as this will create an EMDX file, the code behind and even view the code (which can be very large). If your existing schema is quite large, check out this post , which explains how to deal with large schemas.

When you start the EDM Generator, you will find everything that is not supported.

Having looked at the previous EDMGen2.exe log, we received the following message types:

  • The data type 'sql_variant' is not used; the supported column 'ColumnName' in the table 'TableName' has been excluded.
  • The table / view 'tableName' does not have a primary key. The key was inferred and the definition was created as a table / read-only view
  • The relationship 'RelationshipName' has columns that are not part of the table key on the primary side of the relationship which is not supported, the relationship has been excluded.

We also found that the Linq project actually really crashed Visual Studio because the code file generated by EDM was over 80 mb.

+3
source share

All Articles