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;
2. Assign new Roles to the User.
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.
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.
This simply adds each item to the other’s list of roles/users.