原文:http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx
How do I create an "FK Association"? 添加外键关联
There are a number of ways.
One mainline scenario is when using the tools to infer a model from a database. We have added an option to choose whether "FK Associations" or "Independent Associations" are generated by default. The same is true if you use EdmGen.exe (i.e. our command line tool).
The second mainline scenario for creating FK Associations is creating a model from scratch, aka Model First which is new to .NET 4.0.
Here are the steps involved:
- Create two Entities (say Product and Category) that look something like this:
- Add a property that will be the FK Property: i.e. CategoryID
- Create an association between the two EntityTypes with the correct end multiplicities and NavigationProperty names:
- Double Click the line between the two Entities, that represents the Association, and add a Referential Integrity Constraint:
This is the step that tells the Entity Framework that the CategoryID is an "FK Property" and that the "CategoryProduct" association is an "FK Association". - You end up with something that looks like this:
And you are done you've set up an FK Association. Notice that this association doesn't need to be mapped you simply need to make sure you map all the properties of both Entities.
Very easy.