我正在尝试更新一个简单的模型:
public class DayOff
{
public int Id { get; set; }
public List<User> Users { get; set; }
public DayOff()
{
Users = new List<User>();
}
}
我的服务层是:
public void Update(DayOff dayOff, IEnumerable<int> users)
{
var dayOffToUpdate = _db.DayOff.Include("Users").Single(d => d.Id == dayOff.Id);
dayOffToUpdate.Users.Clear();
_db.Entry(dayOffToUpdate).CurrentValues.SetValues(dayOff);
dayOffToUpdate.Users = _db.User.Where(u => users.Contains(u.Id)).ToList();
foreach (var user in dayOffToUpdate.Users)
{
_db.Entry(user).State = EntityState.Modified;
}
_db.SaveChanges();
}
但它没有更新用户,那我该怎么办?