LINQ to SQL:DataContext.SubmitChanges 未立即更新
我有一个有趣的问题。
执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将对象添加到数据上下文(并将其传播回数据库)与将其添加到从数据库检索的对象的关联实体集合之间存在差异。一旦检索了对象及其关联实体,对数据库中的数据进行的更改将不会反映在检索到的对象中,因为不需要数据库。关键位是公司的
SingleOrDefault()
—— 它强制执行查询并分配检索到的数据。如果尚未预先加载它们,则枚举关联的实体将导致它们被加载。因此,即使在更新数据库之后,先前检索的对象也不会反映更新。但是,您可以将插入的对象添加到公司的数据合规性集合中,然后进行更新,这将是您所期望的。请注意,我认为您仍然需要分配您在支票中使用的关联实体。刷新页面也可以解决问题,因为从数据库中重新查询数据,更新关联的实体集合以及数据上下文中的表。原始(留给上下文)
如果您尝试分配关联实体而不仅仅是其 ID,会怎么样?我相信分配 id 实际上并不会填充正在检查的关联实体,而 SubmitChanges 只是将数据传播回数据库,而不是实际使用关联实体的数据更新表元素。
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.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.
将
c.DataCompliances.Where...
改为DC.DataCompliances.Where...
DC
是数据上下文,定义是什么的c
Change the
c.DataCompliances.Where...
toDC.DataCompliances.Where...
DC
is the data context, what is the definition ofc