实体框架 - 手动添加导航属性
我从我的数据库生成了一个实体框架模型(4.0)。我没有设计数据库,并且对模式没有任何控制,但是有一些表没有定义外键约束,但定义了隐式关系。
例如:
我有一个名为 People 的表,其中包含以下列: 性别ID RaceID
有性别和种族的表,但人员表中没有外键。
当我导入模型时,它没有为这些关系添加导航属性。我尝试手动添加它,但“从角色”和“到角色”被禁用。我不确定如何自己添加关系。我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的 - 事情没那么简单。
您要做的如下:
1 - 右键单击设计器,添加 -> 关联
2 - 设置关联和基数(人物 *..1 性别,人物 *..1 种族)
3 - 进入模型浏览器 -> 关联
4 - 右键单击新创建的关联,然后单击属性
5 - 此处您需要设置密钥和级联选项的端点。确保端点正确。您还可以在此处为隐式导航属性设置引用约束。
6 - 将导航属性映射到相关表/字段。
7 - 验证你的模型,祈祷吧。
Yup - it's not that straightforward.
Here's what you do:
1 - Right click on the designer, Add -> Association
2 - Setup the association and cardinalities (People *..1 Gender, People *..1 Race)
3 - Go into the Model Browser -> Associations
4 - Right click on your newly created associations, click Properties
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 - Map the navigational property to the relevant tables/fields.
7 - Validate your model, cross your fingers.
我发现这篇博文其中提出以下解决方案对我来说非常有用(不幸的是我无法让 RPM1984 在我的情况下工作)。
I came across this blog post which proposes the following solution, which worked great for me (unfortunately I could not get RPM1984's to work in my situation).