LINQ to SQL:DataContext.SubmitChanges 未立即更新

发布于 2024-10-12 03:18:17 字数 864 浏览 3 评论 0原文

我有一个有趣的问题。

执行 DataContext.SubmitChanges() 以一种方式更新 Count(),但不能以另一种方式更新,请参阅下面代码中的我的评论。
(DC 是 DataContext)

  Company c = DC.Companies.SingleOrDefault(x => x.Name == companyName);
  DataCompliance compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

  if (compliances.Count() == 0) // Insert if not exists
  {
    DC.DataCompliances.InsertOnSubmit(new DataCompliance {
      FKCompany = c.Id,
      FKComplianceCriteria = criteria.Id
    });
    DC.SubmitChanges();

    compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

    // At this point DC.DataCompliances.Count() has increased,
    // but compliances.Count() is still 0
    // When I refresh the page however, it will be 1
  }

为什么会发生这种情况?

我需要在插入后立即更新合规性。有人有解决办法吗?

I have a funny problem.

Doing DataContext.SubmitChanges() updates Count() in one way but not in the other, see my comment in the code below.
(DC is the DataContext)

  Company c = DC.Companies.SingleOrDefault(x => x.Name == companyName);
  DataCompliance compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

  if (compliances.Count() == 0) // Insert if not exists
  {
    DC.DataCompliances.InsertOnSubmit(new DataCompliance {
      FKCompany = c.Id,
      FKComplianceCriteria = criteria.Id
    });
    DC.SubmitChanges();

    compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

    // At this point DC.DataCompliances.Count() has increased,
    // but compliances.Count() is still 0
    // When I refresh the page however, it will be 1
  }

Why does that happen?

I need to update compliances immediately after inserting one. Does anyone have a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清秋悲枫 2024-10-19 03:18:17

将对象添加到数据上下文(并将其传播回数据库)与将其添加到从数据库检索的对象的关联实体集合之间存在差异。一旦检索了对象及其关联实体,对数据库中的数据进行的更改将不会反映在检索到的对象中,因为不需要数据库。关键位是公司的 SingleOrDefault() —— 它强制执行查询并分配检索到的数据。如果尚未预先加载它们,则枚举关联的实体将导致它们被加载。因此,即使在更新数据库之后,先前检索的对象也不会反映更新。但是,您可以将插入的对象添加到公司的数据合规性集合中,然后进行更新,这将是您所期望的。请注意,我认为您仍然需要分配您在支票中使用的关联实体。刷新页面也可以解决问题,因为从数据库中重新查询数据,更新关联的实体集合以及数据上下文中的表。

Company c = DC.Companies.SingleOrDefault(x => x.Name == companyName);
var compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

if (compliances.Count() == 0) // Insert one if not exist yet
{
    c.DataCompliances.Add( new DataCompliance
    {
        ComplianceCriteria = criteria
    });
    DC.SubmitChanges();

    compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);
}

原始(留给上下文)

如果您尝试分配关联实体而不仅仅是其 ID,会怎么样?我相信分配 id 实际上并不会填充正在检查的关联实体,而 SubmitChanges 只是将数据传播回数据库,而不是实际使用关联实体的数据更新表元素。

var compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

if (compliances.Count() == 0) // Insert one if not exist yet
{
    DC.DataCompliances.InsertOnSubmit(new DataCompliance {
        FKCompany = c.Id,
        ComplianceCriteria = criteria
    });
    DC.SubmitChanges();

    compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);
}

There's a difference between adding the object to the data context (and propagating it back to the database) and adding it to the associated entity collection of an object you've retrieved from the database. Once you've retrieved an object and its associated entities, making changes to the data in the database won't be reflected in the retrieved object because the database isn't required. The key bit is the SingleOrDefault() on the company -- that forces the query to be executed and the retrieved data assigned. Enumerating the associated entities will cause them to be loaded if they haven't been eagerly loaded already. Thus, even after updating the database, the previously retrieved object won't reflect the update. You can, however, add the inserted object to the company's data compliances collection, then do the update and it will be what you expect. Note that I think you still need to assign the associated entity which you're using in your check. Refreshing the page also solves the problem because the data is requeried from the database, updating the associated entity collections as well as the tables on the data context.

Company c = DC.Companies.SingleOrDefault(x => x.Name == companyName);
var compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

if (compliances.Count() == 0) // Insert one if not exist yet
{
    c.DataCompliances.Add( new DataCompliance
    {
        ComplianceCriteria = criteria
    });
    DC.SubmitChanges();

    compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);
}

Original (left for context)

What if you try assigning the associated entity instead of just its id? I'm believe that assigning the id doesn't actually populate the associated entity that's being checked and SubmitChanges is only propagating the data back to the DB, not actually updating the table element with the data for the associated entity.

var compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

if (compliances.Count() == 0) // Insert one if not exist yet
{
    DC.DataCompliances.InsertOnSubmit(new DataCompliance {
        FKCompany = c.Id,
        ComplianceCriteria = criteria
    });
    DC.SubmitChanges();

    compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);
}
叫思念不要吵 2024-10-19 03:18:17

c.DataCompliances.Where...改为DC.DataCompliances.Where...

DC是数据上下文,定义是什么的c

Change the c.DataCompliances.Where... to DC.DataCompliances.Where...

DC is the data context, what is the definition of c

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文