views:

804

answers:

1

Entity Framework magically interprets the following table structure as a many-to-many relationship.

table foo (int id)
table foo_bar (int foo_id, int bar_id)
table bar (int id)

But if the join table has any additional fields it will instead be interpreted as two one-to-many relationships.

I am using a database in which the join table has a surrogate key as primary key. Because of this EF interprets it as two one-to-many relationships.

table foo (int id)
table foo_bar (int surrogate_pk, int foo_id, int bar_id)
table bar (int id)

Is it possible to modify EF:s interpretation to make it an actual many-to-many relationship in the model? Can it be done using the designer?

+1  A: 

I am positive that this cannot be done using the designer. I don't know if there is a way to do it in EDMX manually, but I have never seen an example of it. One possible workaround might be to not map the surrogate key at all. If you can generate that on the database, you might not need it in your model.

Craig Stuntz
The surrogate key is of no real interest, so that would work nicely. Just deleting the field from the model does not seem to make any difference. Could it be excluded in a more definite way or something?
Erik Öjebo
Deleting the surrogate key in the designer will not remove it from the storage model. You'll have to manually edit the EDMX for that.
Craig Stuntz