为什么不变的行更新?

发布于 2025-02-08 19:38:24 字数 1356 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文