Customize Identity user object contains list object region as with code. When user is registering he can request what are the regions he can work on and what is his role. Once user register email message goes to admin to review user and approve registration request. Until admin approved user is lock. Amin can modify the user region selections and request role if necessary. So Problem come in here. How could I update application user and his regions and role? I tried below but it gave me an exception. Do I need to first update application user then retrieve it and add regions and roles make a send update?( many DB calls)
The property 'Regions' on type 'ApplicationUser' is not a primitive or complex property. The Property method can only be used with primitive or complex properties. Use the Reference or Collection method.)
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; }
}
Regions
public class Region:AuditableBase
{
public string RegionCode { get; set; }
public string Description { get; set; }
public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }
}
Code snippet for Update 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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.