public void AddUsersToRoles(string[] usernames, string[] roleNames) { var users = context.User.Where(x => usernames.Contains(x.Name)); var roles = context.Role.Where(x => roleNames.Contains(x.Name)); foreach (var user in users) { foreach (var role in roles) { user.Roles.Add(role); } } context.SaveChanges(); }
public void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { var users = context .User .Where(x => usernames.Contains(x.Name) && x.Roles.Any(y => roleNames.Contains(y.Name))) .ToList(); var roles = context .Role .Where(x => roleNames.Contains(x.Name)) .ToList(); foreach (var user in users) { var newRoles = user .Roles .Except(roles); user.Roles.Clear(); foreach (var role in newRoles) { user.Roles.Add(role); } context.SaveChanges(); } }
public void Add(T item) { dbSet.Add(item); context.SaveChanges(); }