实体集多个实例的 MVC 实体框架状态行为
我试图理解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您查询 ObjectContext (_aR) 时,默认情况下,它将检查是否有任何已使用相同 EntityKey 从数据库检索到的实例。如果它能找到一个,它将返回已经创建的实例,而不是再次返回数据库。
此行为由 ObjectQuery 的 MergeOption 属性决定,默认为 MergeOption.AppendOnly。相反,您可能需要使用 MergeOption.OverwriteChanges 或 MergeOption.NoTracking,它们每次都会从数据库获取值。
以下是一些可能有用的参考:
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: