Entity Framework - Add Navigation Property Manually

I generated an Entity Framework Model (4.0) from my database. I did not design the database and do not have any control over the schema, but there are a few tables that do not have foreign key constraints defined, but there is an implicit relationship defined.

For example:

I have a table called People that has the following columns: GenderID RaceID

There are tables for both Gender and Race but there is no foreign key in the People table.

When I imported the model it did not add Navigation Properties for these relationships. I tried to add it manually but From Role and To Role are disabled. I'm not sure how to add the relationship myself. How do I do this?

69177 次浏览

Yup - it's not that straightforward.

你要这么做:

右键单击设计器,Add-> 协会

2 - Setup the association and cardinalities (People *..1 Gender, People *..1 Race)

3 - Go into the 模型浏览器 -> 协会

右键单击新创建的关联,单击 物业

在这里,您需要为键和级联选项设置端点。确保端点正确。此处还可以为隐式导航属性设置引用约束。

6 - Map the navigational property to the relevant tables/fields.

验证你的模型,交叉手指。

希望这个能帮上忙。

I came across 这篇博文 which proposes the following solution, which worked great for me (unfortunately I could not get RPM1984's to work in my situation).

  1. 通过设计器背景右键单击上下文菜单添加关联
  2. Set up your Association (be sure to uncheck creation of foreign key)
  3. Right click on the association and choose Properties
  4. 单击参考约束的 ...按钮
  5. 设置内部键之间的关系
  6. 验证(从设计器上下文菜单)
  7. ???
  8. 利润!