How to Update Employee and Identity User with one to one/zero relation

后端 未结 2 1323
陌清茗
陌清茗 2021-01-28 16:45

I am trying to update employee record and want to update identity user too.

If i update Identity User first separately For Example:

UserManager.Update(u         


        
相关标签:
2条回答
  • 2021-01-28 17:20

    Without seeing the exception, I'm not sure what the issue is, but you could trying using an attached entity and set values like the following.

     var dbEmployee = Context.Emplyoees.SingleOrDefault(s => s.Id == employee.Id);
     if (dbEmployee!= null)
     Context.Entry(dbEmployee).CurrentValues.SetValues(employee);
    
    0 讨论(0)
  • 2021-01-28 17:27

    The User employee service should be

    public bool UpdateEmployee(Employee employee)
    {
        var existingEmployee = Context.Emplyoees.FirstOrDefault(s => s.Id == employee.Id);
        if (existingEmployee != null)
        {
            //do the update to the database 
            Context.Entry(existingEmployee).CurrentValues.SetValues(employee);
            Context.Entry(existingEmployee).State = System.Data.Entity.EntityState.Modified;
            return Context.SaveChanges() > 0;
        }
        else return false;
    }
    
    0 讨论(0)
提交回复
热议问题