Attach it before modifying, modify, then save changes.
public void UpdateEmployee(int empId, int deptId, int level)
{
Employee emp = GetEmployee(empId);
using (var dbContext = new MyDbContext())
{
dbContext.Employees.Attach(emp);
emp.DeptId = deptId;
emp.Level = level;
//this is having no effect?
dbContext.SaveChanges()
}
}
You could also attach it, and then set the state to Modified:
public void UpdateEmployee(int empId, int deptId, int level)
{
Employee emp = GetEmployee(empId);
emp.DeptId = deptId;
emp.Level = level;
using (var dbContext = new MyDbContext())
{
dbContext.Employees.Attach(emp);
dbContext.Entry(emp).State = EntityState.Modified;
dbContext.SaveChanges()
}
}
And if you only want to send those fields in the update instead of all columns:
public void UpdateEmployee(int empId, int deptId, int level)
{
Employee emp = GetEmployee(empId);
emp.DeptId = deptId;
emp.Level = level;
using (var dbContext = new MyDbContext())
{
dbContext.Employees.Attach(emp);
dbContext.Entry(emp).Property(p => p.DeptId).IsModified = true;
dbContext.Entry(emp).Property(p => p.Level).IsModified = true;
dbContext.SaveChanges()
}
}
GetEmployeemethod work ? are you sure it returns the correct result?