如何使用Entity Framework 6使用导航属性更新IdentityUser

时间:2019-05-07 10:11:24

标签: c# asp.net asp.net-mvc-5

我想与员工记录一起更新IdentityUser,

IdentityUser类别

public class AppUser : IdentityUser<string, AppUserLogin, AppUserRole, AppUserClaim>, IUser<string>
{
        public AppUser()
        {
            this.Id = Guid.NewGuid().ToString();
        }
        public async Task<ClaimsIdentity>
            GenerateUserIdentityAsync(UserManager<AppUser, string> manager)
        {
            var userIdentity = await manager
                .CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            return userIdentity;
        }
        [Required]
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool IsActive { get; set; }
        public virtual Employee Employee {get; set;}
}

员工阶层

public class Employee
{
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }

    public string AppUserId { get; set; }

    [ForeignKey("AppUserId")]
    public virtual AppUser AppUser { get; set; }
}

在这里我想一起更新我的员工和identityUser。

public JsonResult Edit(EmployeeVM evm, AppUserVM appUserVM)
{
            ModelState.Remove(nameof(evm.CreatedDate));
            var jsonResult = new JsonResult();
            jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (ModelState.IsValid)
            {
                appUserVM.UserName = appUserVM.Email;
                var user = UserManager.FindById(evm.UserId);
                user.Email      = appUserVM.Email;
                user.UserName      = appUserVM.UserName;
                user.FirstName  = appUserVM.FirstName;
                user.LastName      = appUserVM.LastName;
                user.IsActive   = appUserVM.IsActive;
                user.PhoneNumber   = appUserVM.PhoneNumber;
                var employee = Mapper.Map<Employee>(evm);
                employee.AppUser = user;
                employee.Id = evm.Id;
                employee.AppUserId = user.Id;
                try
                {
                    if(userService.UpdateEmployee(employee))
                        jsonResult.Data = new { Success = true, message = "Success" };
                }
                catch (Exception ex)
                {

                    jsonResult.Data = new { Success = false, message = ex.Message };
                }
            }
            else
            {
                jsonResult.Data = new { Success = false, message = ModelErrors() };
            }

   return jsonResult;
}


public bool UpdateEmployee(Employee employee)
{
    Context.Entry(employee).State = System.Data.Entity.EntityState.Modified;
    return Context.SaveChanges() > 0;
}



我如何也可以使用identityUser更新员工。 例如:

employee.name = "abc";
employee.age = "30";
employee.cellnumber: "1233456";

现在的IdentityUser属性

employee.Appuser.Email = "abc@abc.com"
employee.Appuser.UserName = "abc";

现在我想更新identityuser也应该更新的员工。

0 个答案:

没有答案