ADO.Net EF – how to define foreign key relation in model first approach?

First of all your model is probably wrong. ConsoleType and Game is not in one-to-one relation (unless you have single game for each console type). I expect 1 console can have multiple games. So it should be one-to-many. In reality game can be released on multiple platforms so it should be many-to-many.

You got unvanted column because your relation between ConsoleType and Game doesn’t know that it should use Console property as a foreign key. This happens if you use independent association. Independent associations are used by default when you draw them from one entity to other entity in Entity designer. You must use foreign key association.

Start with this set up (draw association from ConsoleType to Game – you must have one-to-many relation):

enter image description here

Select relation between ConsoleType and Game an in properties click on Referential Constraint:

enter image description here

In Referential Constraint dialog just set up relation:

enter image description here

Save your model and generate database again.

Leave a Comment