public ApplicationUser Create(ApplicationUser applicationUser) { if (IsPermissionAllowed(applicationUser)) { int tenantId = applicationUser.UserTenants.FirstOrDefault().TenantId; applicationUser.UserTenants = null; bool userSucceeded = _accountProvider.Create(applicationUser, applicationUser.TemporaryCode); if (userSucceeded) { bool roleSucceeded = _accountProvider.AddToRole(applicationUser.Id, applicationUser.TemporaryRole); Tenant tenant = _db.Tenants.FirstOrDefault(t => t.TenantId == tenantId); ApplicationUser reloadedUser = _db.Users.Include(t => t.UserTenants).FirstOrDefault(u => u.Id == applicationUser.Id); reloadedUser.UserTenants = new List <Tenant> { tenant }; _db.Entry(reloadedUser).State = EntityState.Modified; _db.SaveChanges(); } else { Errors = new string[] { "User name already exists" }; } } return(applicationUser); }