为什么不变的行更新?
EF更新行,即使它们保持不变。我无法理解这种行为。涉及两个DB模型:日与活动(关系1-1)。该代码被简化以使情况更加清晰。
public class Day
{
[Key]
public int DayId { get; set; }
...
public Activity? Activity { get; set; } //Day may or may not have an activity
[Timestamp]
public byte[] RowVersion { get; set; }
}
public class Activity
{
[Key]
public int ActivityId { get; set; }
public int DayId { get; set; } //Foreign key
public int Property { get; set; }
...
[Timestamp]
public byte[] RowVersion { get; set; }
}
然后在单个上下文中从数据库中检索数据:
List<Day> GetDays(...)
{
ctx = _factory.CreateDbContext(); //Global attribute
return from day in ctx.Days.Where(day => ...conditions...)
.Include(x => x.Activity)
select day).ToList();
}
数据已处理:
List<Day> list = GetDays(...);
foreach(Day other_day in some_other_day_list)
{
list.Single(day => day.DayId == other_day.DayId).Activity.Property = other_day.Activity.Property;
}
SaveDates(list);
保存数据:
SaveDates(List<Day> list)
{
ctx.Days.UpdateRange(list); //ctx is global attribute
ctx.SaveChanges();
ctx.Dispose();
}
此操作的结果是全天都在更新(更改划分),并且只有每天修改的活动正在更新。尽管只有几天已经修改了活动,但所有天数都会更新。我无法搜索答案,或者我不知道要问的正确问题。
EF updates rows even though they remain unchanged. I can't understand this behavior. There are two DB Models involved: Day and Activity (relation 1-1). The code is simplified to make things more clear.
public class Day
{
[Key]
public int DayId { get; set; }
...
public Activity? Activity { get; set; } //Day may or may not have an activity
[Timestamp]
public byte[] RowVersion { get; set; }
}
public class Activity
{
[Key]
public int ActivityId { get; set; }
public int DayId { get; set; } //Foreign key
public int Property { get; set; }
...
[Timestamp]
public byte[] RowVersion { get; set; }
}
Data is then being retrieved from DB in a single context:
List<Day> GetDays(...)
{
ctx = _factory.CreateDbContext(); //Global attribute
return from day in ctx.Days.Where(day => ...conditions...)
.Include(x => x.Activity)
select day).ToList();
}
Data is processed:
List<Day> list = GetDays(...);
foreach(Day other_day in some_other_day_list)
{
list.Single(day => day.DayId == other_day.DayId).Activity.Property = other_day.Activity.Property;
}
SaveDates(list);
Data is Saved:
SaveDates(List<Day> list)
{
ctx.Days.UpdateRange(list); //ctx is global attribute
ctx.SaveChanges();
ctx.Dispose();
}
The result of this operation is that ALL Days are being updated (RowVersion is changed) and ONLY the modified Activities of each Day are being updated. All Days are updated despite the fact that only some of Days have modified Activities. I can't google the answer, or I don't know the right question to ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论