AutoDetectChangesEnabled = false时更新相关数据

时间:2012-12-05 23:50:22

标签: entity-framework

我正在尝试更新一个简单的模型:

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();
}

但它不是在更新用户,所以我该怎么办?

0 个答案:

没有答案