实体集多个实例的 MVC 实体框架状态行为

发布于 2024-08-16 13:38:57 字数 1030 浏览 6 评论 0原文

我试图理解 EF 实体模型的实体集实例之间的关系(该模型是由实体设计器创建的)。基本上我最终得到 1 个逻辑事务,其中有 2 个实体的 Repository 类的实例。一个实例中成功提交的数据(通过直接 SSMS 查询 SQLServer 确认)不会对另一实例可见。大致流程如下:

ARepository _aR = new ARepository();

A a = _aR.Find(id);   //Find does something like:  return db.ASet.Where(x => x.id == id);

b.BeginAcctBal = a.AcctBal;  

brepo.AddTxn(params ... n);  //Creates and saves n txns, and creates its own ARepository instance that updates its instance of AcctBal, which I can see happening in the DB)

A a = _aR.Find(id);  //The hope is this gets the updated AcctBal after committed by AddTxn, but it doesn't).

b.EndAcctBal = a.AcctBal;  // Still contains the starting balance value.

现在,如果我将 ARepository _aR = new ARepository(); 立即放在 AddTxn 之后,则后续代码确实会获取 AddTxn 之后的 AcctBal 值。

问题:

为什么 db.ASet.Where(x => x.id == id); 不从数据库重新加载?它真的总是从创建 _aR 实例时的快照中读取吗?

如果_aR是快照,有什么方法可以重新加载它吗?

如果 _aR 是快照,如何维护事务完整性?更具体地说,我是否需要做一些事情来维护它,或者 EF 和 MVC 1.0 的组合是否为我带来了这种事务魔力?

I'm trying to understand the relationship between entityset instances of an EF Entities model (the model was created by Entity Designer). Basically I end up with 1 logical transaction having 2 instances of an entity's Repository class. Data committed successfully (confirmed by direct SSMS query to SQLServer) in one instance does not become visible to the other instance. Here's roughly the flow:

ARepository _aR = new ARepository();

A a = _aR.Find(id);   //Find does something like:  return db.ASet.Where(x => x.id == id);

b.BeginAcctBal = a.AcctBal;  

brepo.AddTxn(params ... n);  //Creates and saves n txns, and creates its own ARepository instance that updates its instance of AcctBal, which I can see happening in the DB)

A a = _aR.Find(id);  //The hope is this gets the updated AcctBal after committed by AddTxn, but it doesn't).

b.EndAcctBal = a.AcctBal;  // Still contains the starting balance value.

Now if I put ARepository _aR = new ARepository(); immediately after the AddTxn, then the subsequent code does indeed get the post-AddTxn AcctBal value.

Questions:

Why does db.ASet.Where(x => x.id == id); not reload from the db? Is it really always reading from a snapshot from when the _aR instance is created?

If _aR is a snapshot, is there any way to reload it?

If _aR is a snapshot, how is transactional integrity being maintained? More specifically, do I need to do something to maintain it, or does the combination of EF and MVC 1.0 do this transactional magic for me?

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

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

发布评论

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

评论(1

思念满溢 2024-08-23 13:38:57

当您查询 ObjectContext (_aR) 时,默认情况下,它将检查是否有任何已使用相同 EntityKey 从数据库检索到的实例。如果它能找到一个,它将返回已经创建的实例,而不是再次返回数据库。

此行为由 ObjectQuery 的 MergeOption 属性决定,默认为 MergeOption.AppendOnly。相反,您可能需要使用 MergeOption.OverwriteChangesMergeOption.NoTracking,它们每次都会从数据库获取值。

以下是一些可能有用的参考:

  1. MergeOptions 讨论
  2. 对象上下文的刷新方法
  3. < a href="http://msdn.microsoft.com/en-us/library/bb738618.aspx" rel="nofollow noreferrer">保存更改和管理并发(实体框架)
  4. 类似论坛帖子

When you query your ObjectContext (_aR), by default, it will check for any instances that have already been retrieved from the DB with the same EntityKey. If it can find one, it will return the already created instance, rather than going back to the DB again.

This behavior is determined by the ObjectQuery's MergeOption propery, which defaults to MergeOption.AppendOnly. Instead, you may want to use MergeOption.OverwriteChanges or MergeOption.NoTracking, which will get the values from the DB everytime.

Here are some references which may be helpful:

  1. Discussion of MergeOptions
  2. Object Context's Refresh Method
  3. Saving Changes and Managing Concurrency (Entity Framework)
  4. Similar forum post
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文