实体框架 - 手动添加导航属性

发布于 2024-10-07 16:17:17 字数 238 浏览 0 评论 0原文

我从我的数据库生成了一个实体框架模型(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无言温柔 2024-10-14 16:17:17

是的 - 事情没那么简单。

您要做的如下:

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.

爱给你人给你 2024-10-14 16:17:17

我发现这篇博文其中提出以下解决方案对我来说非常有用(不幸的是我无法让 RPM1984 在我的情况下工作)。

  1. 通过设计器背景添加关联右键单击上下文菜单
  2. 设置您的关联(确保取消选中外键的创建)
  3. 右键单击关联并选择“属性”
  4. ”的 ... 按钮
  5. 单击“参考约束集 提高验证中按键之间的关系
  6. (从设计器上下文菜单)
  7. ???
  8. 利润!

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).

  1. Add an Association via designer background right click contextual menu
  2. Set up your Association (be sure to uncheck creation of foreign key)
  3. Right click on the association and choose Properties
  4. Click on the ... button for Referential Constraint
  5. Set up the relation between the keys within
  6. Verify (from the designer contextual menu)
  7. ???
  8. Profit!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文