在实体框架中添加/删除多对多关联
我的示例数据库中有三个表:
用户
- ID
- 用户名
- 密码
角色
- ID
- 名称
- 说明
UserRoles
- UserID
- RoleID
UserRoles 是一个用于模拟多对多关系的查找表。向该表添加记录允许将用户和角色中的记录关联起来。我的问题是,在实体框架中,它正确地将其解释为多对多关系并抽象出该查找表。
这在大多数情况下都很有效,但是当我想从该查找表中添加/删除条目时,我不确定该怎么做。我可以删除角色或用户,但这实际上会删除对象,而不仅仅是它们之间的关联。
我确实知道有一个选项可以将虚拟列添加到 UserRoles 查找表中。这将迫使实体框架将查找表变成一个成熟的实体,允许我将它们作为单独的对象添加和删除。但我不需要虚拟列,这看起来像是一个黑客。我正在寻找更好的建议。
I have three tables in my sample database:
Users
- ID
- Username
- Password
Roles
- ID
- Name
- Description
UserRoles
- UserID
- RoleID
UserRoles is a lookup table to simulate a many to many relationship. Adding records to this table allows one to associate records in Users and Roles. My problem is that in Entity Framework it correctly interprets this as a many to many relationship and abstracts away that lookup table.
This works great most of the time, but I'm not sure what to do when I want to add/delete entries from that lookup table. I can delete roles or users but that actually deletes the objects not just their association with each other.
I do know of one option to add a dummy column to the UserRoles lookup table. That will force Entity Framework to turn the lookup table into a full-blown entity, allowing me to add and remove them as lone objects. But I have no need for a dummy column and this seems like a hack. I am looking for better suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它应该看起来像这样:
删除关系
添加关系
It should look something like this:
To Remove Relationship
To Add Relationship
您可以在实体上使用导航属性:(
假设 u 是用户对象):
编辑 - 根据 Slauma 的评论添加了 SaveChanges。
You can use the navigation properties on the entities:
(assuming u is a User object):
EDIT - Added SaveChanges based on Slauma's comment.
我之前已经解决了这个问题,只需将私钥标识符自动增量列添加到查找表中,因为实体框架将始终隐藏仅包含 2 列且末表具有外键的查找表。有时您需要通过实体框架直接添加查找条目,这将帮助您实现这一目标。
问题作者的更新
我只是想提供我自己对这个答案的实现的更新。我向查找表添加了一个标识列,并在两个外键列上创建了一个唯一键,以防止表中出现重复的关系条目。我的模型现在看起来像这样:
http://www.codetunnel.com/content/images /ManyToManyDynamic.jpg
唯一糟糕的是获取所有关联角色的集合,我必须这样做:
这需要更多的工作,但除非有相当于
user.Roles.Remove(role)
(类似于user.Roles.Associate(existingRoleEntity)
),那么这是我唯一的选择。更新:
可以通过以下方式实现:
您始终可以使用 公共分部类 扩展
User
以获得使用上述内容返回所有角色的属性。单击链接以获取我在另一个问题上提供的公共部分类
内容的详细信息。I have solved this problem before by simply adding an Private Key Identifier Auto-Increment column to the lookup table as
Entity Framework
will always hide lookup tables that only contain 2 columns with foreign keys to the end tables. Sometimes you need to add a lookup entry directly yourself viaEntity Framework
and this will help you achieve that.Update From Question Author
I just wanted to provide an update on my own implementation of this answer. I added an identity column to the lookup table and created a unique key over the two foreign key columns to prevent duplicate relationship entries in the table. My model now looks like this:
http://www.codetunnel.com/content/images/ManyToManyDynamic.jpg
The only thing that sucks is to get a collection of all associated Roles I would have to do this:
It's a little more work but unless there is an equivalent to
user.Roles.Remove(role)
(something likeuser.Roles.Associate(existingRoleEntity)
) then this is my only option.Update:
Can be achieved via:
You can always use a public partial class to extend
User
to have a property to return all roles using the above. Click the link for details of thepublic partial class
stuff I gave on another question.