实体框架 - 更改关系多重性

发布于 2024-08-11 13:05:18 字数 410 浏览 3 评论 0原文

我的数据库中有一个表 [用户] 和另一个表 [销售人员]。 [Salesperson] 定义了一个唯一 UserID,它通过外键映射到 [User].UserID。当我使用实体框架生成模型时,我在 [User]-[Salesperson] 之间获得一对多关系,这意味着每个用户都有一个“销售人员集合”,但我想要的是 0..1-to -1 关系,其中每个用户都有对“销售人员”的可为空引用。

我尝试摆弄 XML 并更改关联的多重性设置,但这只会产生构建错误。我想要实现的目标与在 [User] 中引用 [Salesperson].SalespersonID 的可空 SalespersonID 没有什么不同,但因为销售人员仅针对特定用户存在,所以感觉我会弄乱我的 [User] 表结构使关系在实体框架中指向正确的方向。

我能做些什么来改变这种关系的多重性吗?

I have a table [User] and another table [Salesperson] in my database. [Salesperson] defines a unique UserID which maps to [User].UserID with a foreign key. When I generate the model with Entity Framework I get a 1-to-Many relationship between [User]-[Salesperson], meaning that each User has a "Collection of Salesperson", but what I want is a 0..1-to-1 relationship where each User has a nullable reference to a "Salesperson".

I tried fiddling around with the XML and changing the association's multiplicity settings, but that only produced build errors. What I am trying to achieve is no different than having a nullable SalespersonID in [User] that references [Salesperson].SalespersonID, but because salespeople only exist for specific users it feels like I'd be muddying up my [User] table structure just to get the relationship to point the right way in Entity Framework.

Is there anything I can do to change the multiplicity of the relationship?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

风尘浪孓 2024-08-18 13:05:18

销售人员本身的PK设置为用户的FK。然后,EF 的 GUI 设计者将获得正确的基数,因为 PK 是唯一的。

Make the PK of Salesperson itself a FK to User. The EF's GUI designer will then get the cardinality correct, since PKs are unique.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文