Entity Framework 4 Code First,复杂的多对多关系

发布于 2024-11-17 00:17:39 字数 281 浏览 3 评论 0原文

我在 EF 中看到了几个使用代码优先的多对多关系的示例,但我遇到了一种不确定如何解决的情况。

我的应用程序有三个 POCO 类:帐户、用户和角色。每个用户都可以是多个帐户的成员,并且每个帐户都有一个独特的角色。例如,用户 A 可以属于帐户 A 的角色 A 和帐户 B 的角色 B。本质上,帐户和用户之间存在多对多关系,并且每个这些关系还具有分配给每个帐户的角色 -用户组合。

在我当前的非代码优先设置中。我有一个表,其中包含用户 ID、帐户 ID 和角色 ID。

有没有办法使用代码优先来做到这一点?

I've seen several examples of many to many relationships in EF using code-first, but I've run into a situation that I'm not sure how to solve.

My app has three POCO classes, Account, User and Role. Every user can be a member of multiple accounts and has a unique role for each one. For example, User A can be in Role A for Account A and Role B for Account B. Essentially, there is a many to many relationship between accounts and users, and each of these relationships also has a role that is assigned to each account-user combination.

In my current, non code-first setup. I have a table that has a user id, account id and role id.

Is there a way to do this using code-first?

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

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

发布评论

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

评论(1

Bonjour°[大白 2024-11-24 00:17:39

是的,您可以使用 CodeFirst 执行此操作,您将需要一个额外的类(实体)来表示此关系表。

您的用户将拥有一组具有角色和帐户的帐户成员资格。

User
--- has a collection of AccountMemberShips

AccountMemberShip
--- has a  Role
--- has an Account
--- has a  User

Account
--- has a collection of AccountMemberShips

Role
--- has a collection of AccountMemberShips

yes you can do this with CodeFirst you will need an extra class (entity) to represent this relationship table.

where your user will have a collection of account memberships which have a role and an account.

User
--- has a collection of AccountMemberShips

AccountMemberShip
--- has a  Role
--- has an Account
--- has a  User

Account
--- has a collection of AccountMemberShips

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