简体   繁体   English

实体框架代码优先:多对多关系; 从用户中删除一些角色

[英]Entity Framework Code First: Many-to-Many relationship; Remove some Roles from the User

I have a Many-to-Many User/Role relationship. 我有一个多对多用户/角色关系。

It's OK, when I insert a new user into the database and add it in some roles. 可以,当我在数据库中插入新用户并将其添加为某些角色时。

But, how to remove some roles from the user? 但是,如何从用户中删除某些角色? I don't want to completely remove user or role, I only want to: 1. Remove some Roles from the User; 我不想完全删除用户或角色,我只想:1.从用户中删除一些角色; 2. Assign new Roles to the User. 2.为用户分配新角色。

My Classes: 我的课程:

public class User
{
    public int Id { get; set; }    
    public virtual string UserName { get; set; }
    public virtual string Password { get; set; }

    public List<Role> Roles { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public virtual string Name { get; set; }

    public List<User> Users { get; set; }
}

I'm not entirely clear on what you are asking for, but I think it is that you want to be able to remove a role from a user, and that user from the appropriate role? 我对您的要求尚不完全清楚,但我认为这是您希望能够从用户中删除角色,以及将该用户从相应角色中删除?

If so, you will know the user and role, and for this simple case want to make use of the List.Remove function. 如果是这样,您将知道用户和角色,并且在这种简单情况下,您想使用List.Remove函数。

void RemUserRole(User u, Role r)
{
    u.Roles.Remove(r);
    r.Users.Remove(u);
}

This will remove the items from the relevant list of each Role and User. 这将从每个角色和用户的相关列表中删除项目。

Edit: 编辑:

To add new roles to users, you use the List.Add method. 要向用户添加新角色,请使用List.Add方法。

void AddUserRole(User u, Role r)
{
    u.Roles.Add(r);
    r.Users.Add(u);
}

This simply adds each item to the other's list of roles/users. 这只是将每个项目添加到对方的角色/用户列表中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Entity Framework 4.1 - Code First:多对多关系 - Entity Framework 4.1 - Code First: many-to-many relationship 如何在实体框架中添加或删除多对多关系? - How to add or remove a many-to-many relationship in Entity Framework? 编写第一个实体框架(EF6)的代码,在初始创建后添加多对多关系 - Code first Entity Framework (EF6) Adding Many-to-Many relationship after initial create 通过具有多对多关系的实体框架代码优先方法为 SQL Server 播种 - Seeding SQL Server by Entity Framework code-first approach with many-to-many relationship Code First Entity Framework中的Seed方法未填充多对多关系中的链接表 - The link table in Many-to-many relationship is not populated by the Seed method in Code First Entity Framework 如何首先使用Entity Framework代码创建多对多关系? - How do I create a many-to-many relationship with Entity Framework code first? 实体框架核心代码优先:级联删除多对多关系 - Entity Framework Core Code-First: Cascade delete on a many-to-many relationship 自引用多对多递归关系代码优先实体框架 - Self-referencing many-to-many recursive relationship code first Entity Framework 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 如何在实体代码优先中以多对多关系为数据库播种 - How to seed the database with a many-to-many relationship in Entity Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM