EF更新ApplicationUser(继承自IdentityUser),包含多对多关系集合

时间:2016-09-10 10:43:09

标签: asp.net .net entity-framework asp.net-web-api entity-framework-6

自定义标识用户对象包含列表对象区域和代码一样。当用户注册时,他可以请求他可以处理的区域是什么以及他的角色是什么。一旦用户注册电子邮件消息进入管理员审查用户并批准注册请求。管理员批准的用户锁定之前。如有必要,Amin可以修改用户区域选择和请求角色。所以问题就在这里。我如何更新应用程序用户及其区域和角色?我在下面试过,但它给了我一个例外。我是否需要首先更新应用程序用户然后检索它并添加区域和角色进行发送更新?(许多数据库调用)

“ApplicationUser”类型上的“Regions”属性不是原始属性或复杂属性。 Property方法只能与原始或复杂属性一起使用。使用参考或收集方法。)

ApplicationUser

public class ApplicationUser : IdentityUser
    {
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    //Extended Properties
    public DateTime? BirthDate { get; set; }

    //Key Mappings
    public virtual ICollection<Region> Regions { get; set; }

    public virtual string DisplayName { get; set; }

    public virtual string UserAccountApproverId { get; set; }
    public virtual ApplicationUser UserAccountApprover { get; set; }
}

地区

   public class Region:AuditableBase
   {
    public string RegionCode { get; set; }

    public string Description { get; set; }

    public virtual ICollection<ApplicationUser> ApplicationUsers { get; set;   }

  }

更新ApplicationUser的代码段

public int ApproveNewUser(UserModel userModel)
    {
        try
        {
            ApplicationUser user = new ApplicationUser()
            {
                Id = userModel.Id,
                UserName = userModel.EmailAddress,
                LockoutEnabled = false
            };
            _ctx.Users.Attach(user);

            var entry = _ctx.Entry(user);
            entry.Property(e => e.LockoutEnabled).IsModified = true;

            if (userModel.CheckedRegionsUpdated)
            {
                AddRegionsToUser(userModel.SelectedRegions, user);
                entry.Property(e => e.Regions).IsModified = true;
            }

            return _ctx.SaveChanges();
        }
        catch (OptimisticConcurrencyException ex)
        {
            var objectContext = ((IObjectContextAdapter)_ctx).ObjectContext;
            objectContext.Refresh(RefreshMode.ClientWins, _ctx.Users);
            return _ctx.SaveChanges();
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

    private void AddRegionsToUser(IList<Region> regionsToAdd, ApplicationUser appUser)
    {
        appUser.Regions = new List<Region>();

        var regionsIds = regionsToAdd.Select(x => x.Id).ToArray<int>();

        List<Region> regionssFromDb =
            this._ctx.Regions.Where(rg => regionsIds.Contains(rg.Id)).ToList();

        foreach (Region region in regionssFromDb)
        {
            appUser.Regions.Add(region);
        }
    }

1 个答案:

答案 0 :(得分:0)

这是链接,它给了我一个答案。

Many to Many Relationships not saving

非常感谢Slauma