实体框架 - 手动添加导航属性 [英] Entity Framework - Add Navigation Property Manually
问题描述
我从我的数据库生成了一个实体框架模型 (4.0).我没有设计数据库,对schema没有任何控制权,但是有几个表没有定义外键约束,但是定义了一个隐式关系.
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.
例如:
我有一个名为 People 的表,其中包含以下列:性别ID种族ID
I have a table called People that has the following columns: GenderID RaceID
Gender 和 Race 都有表,但 People 表中没有外键.
There are tables for both Gender and Race but there is no foreign key in the People table.
当我导入模型时,它没有为这些关系添加导航属性.我尝试手动添加它,但 From Role 和 To Role 被禁用.我不确定如何自己添加关系.我该怎么做?
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?
推荐答案
是的 - 没那么简单.
Yup - it's not that straightforward.
这就是你要做的:
1 - 右键单击设计器,添加 -> 关联
1 - Right click on the designer, Add -> Association
2 - 设置关联和基数(人员 *..1 性别,人员 *..1 种族)
2 - Setup the association and cardinalities (People *..1 Gender, People *..1 Race)
3 - 进入模型浏览器 -> 关联
4 - 右键单击您新创建的关联,单击属性
4 - Right click on your newly created associations, click Properties
5 - 在这里您需要为密钥和级联选项设置端点.确保您获得正确的端点.您还可以在此处为隐式导航属性设置引用约束.
5 - Here you need to setup the endpoints for the key and cascade options. Make sure you get the endpoints correct. You can also setup a referential constraint here for your implicit navigational property.
6 - 将导航属性映射到相关表/字段.
6 - Map the navigational property to the relevant tables/fields.
7 - 验证您的模型,交叉手指.
7 - Validate your model, cross your fingers.
希望这会有所帮助.
这篇关于实体框架 - 手动添加导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!