コード例 #1
0
ファイル: EFRole.cs プロジェクト: lihachsk/Snowboard
        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();
        }
コード例 #2
0
        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();
            }
        }
コード例 #3
0
 public void Add(T item)
 {
     dbSet.Add(item);
     context.SaveChanges();
 }