为什么 Entity Framework 6.x 不缓存结果? [英] Why does Entity Framework 6.x not cache results?

查看:28
本文介绍了为什么 Entity Framework 6.x 不缓存结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我误解了 DbContextDbSet 所做的缓存,但我的印象是有一些缓存会继续.当我运行以下代码时,我看到了意想不到的行为:

Perhaps I am misunderstanding the caching that DbContext and DbSet does but I was under the impression that there was some caching that would go on. I'm seeing behavior that I wouldn't expect when I run the following code:

var ctx = CreateAContext();
var sampleEntityId = ctx.SampleEntities.Select(i => i.Id)
                                       .Single(i => i == 3); //Calls DB as expected
var cachedEntityId = ctx.SampleEntities.Select(i => i.Id)
                                       .Single(i => i == 3); //Calls DB unexpectedly

这是怎么回事?我认为你从 DbSet 得到的部分是它会在查询数据库之前首先检查本地缓存以查看该对象是否存在.我在这里缺少某种配置选项吗?

What's going on here? I thought that part of what you get from DbSet is that it would first check the local cache to see if that object exists before querying the database. Is there just some sort of configuration option I am missing here?

推荐答案

@emcas88 想说的是EF 只会在您使用 DbSet 上的 .Find 方法时检查缓存.

What @emcas88 is trying to say is that EF will only check the cache when you use the .Find method on DbSet.

使用 .Single.First.Where 等不会缓存结果,除非您使用二级缓存.

Using .Single, .First, .Where, etc will not cache the results unless you are using second-level caching.

这篇关于为什么 Entity Framework 6.x 不缓存结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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