实体框架6.0多对多插入/更新问题

时间:2015-12-26 14:00:39

标签: sql-server entity-framework asp.net-mvc-5

尝试针对角色组保存角色列表但是没有插入第三个表?任何人都可以帮助我错过的东西吗?

public async Task<string> AssignSave(List<ApplicationRoles> Roles , int Id)
    {
        using (var context = new ApplicationDbContext())
        {
            var rolegroup = await (from _rolegroup in context.RoleGroup.Include(x => x.Roles)
                                   where _rolegroup.FK_CompanyProfile_ID.Equals(CompanyId) && _rolegroup.Id.Equals(Id)
                                   select _rolegroup).FirstOrDefaultAsync();
            context.Entry<RoleGroup>(rolegroup).State = EntityState.Modified;
            rolegroup.Roles.Clear();
            foreach (var role in Roles)
            {
                var _role = await context.ApplicationRoles.FindAsync(role.Id);
                rolegroup.Roles.Add(_role);
                context.Entry<ApplicationRoles>(_role).State = EntityState.Modified;
            }
            int y = await (context.SaveChangesAsync());
            if (y > 0)
                return Success;
            else
                return Error;
        }
    }

0 个答案:

没有答案