DbContext的内部缓存(?) [英] DbContext's internal caching (?)

查看:292
本文介绍了DbContext的内部缓存(?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了自己的上下文,继承自DbContext。假设我在我的数据库中的[帖子]表中有1个帖子。考虑这种情况:


  1. 我要求DbContext为第一次的这个帖子。而且DbContext会按预期的方式返回它。

  2. 我手动修改了[帖子]表格中的一列。

  3. 站点=我再次请求DbContext发送此帖子。

  4. DbContext返回一个具有旧值的帖子!

我研究了SQL Profiler,每次刷新我的网站时都会触发数据库,​​所以为什么返回的帖子有一个旧的值?我猜DbContext试图非常聪明,并且有一些内部的缓存机制,但是告诉我他很聪明很好...



有人可以解释一下对我来说

解决方案

你好吗?如果您知道值已更改,则可以使用ServerWins或ClientWins刷新上下文缓存值:



http://msdn.microsoft.com/en-us/library/vstudio/bb738618(v = 100).aspx

  //通过刷新
//对象上下文来解决并发冲突重新保存更改。
context.Refresh(RefreshMode.ClientWins,orders);

然而,真正的问题归结于何时以及为什么会发生这种情况,首先是与上下文的生命周期相关。也许你可能会过度使用相同的语境?记住,SQL Server等有自己的缓存机制,因此实体框架不是池塘中唯一一个尝试进行缓存的项目。



从,以下链接我们收集一些提示,如果您正在关注,您可能不会遇到此问题。 http://msdn.microsoft.com/en-us/data/jj729737.aspx




  • 当您加载更多对象时,上下文的内存消耗可能会快速增长

  • 发生并发相关问题的机会随着生命周期的增加而增加。

  • 使用Web应用程序时,每个请求使用上下文实例

  • 使用Windows窗体时,使用每个窗体的上下文


I created my own context which inherits from DbContext. Let's assume I have 1 post in my [Posts] table in the database. Consider this scenario:

  1. I ask DbContext for this only post for the first time. And DbContext returns it as expected.
  2. I change one column in [Posts] table manually.
  3. I refresh my site = I ask DbContext for this post again.
  4. DbContext returns a post which has old value for this specific column!

I looked into SQL Profiler and the database IS hit every time I refresh my site, so why the returned post has an old value? I guess DbContext is trying to be very clever and has some internal caching mechanism, but it would be nice to inform me that he's so clever...

Can someone explain this to me?

解决方案

How are you "refreshing"? If you know a value has changed, you can Refresh the context cached value using ServerWins or ClientWins :

http://msdn.microsoft.com/en-us/library/vstudio/bb738618(v=vs.100).aspx

// Resolve the concurrency conflict by refreshing the 
// object context before re-saving changes. 
context.Refresh(RefreshMode.ClientWins, orders);

However, the real question comes down to when and why this would happen in the first place, which is related to the lifetime of the context. Perhaps you could be over-using the same context? Remember that SQL Server and etc have their own caching mechanism, and thus Entity Framework isn't the only fish in the pond of items trying to do some caching.

From, the following link we gather some tips, which if you were following, you likely wouldn't run into this problem. http://msdn.microsoft.com/en-us/data/jj729737.aspx

  • As you load more objects, memory consumption of the context may increase rapidly
  • Chances of running into concurrency related issues increases with expanded lifetime
  • When working with Web Applications, use a context instance per request
  • When working with Windows Forms, use a context per form

这篇关于DbContext的内部缓存(?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆