问题描述
也许我误解缓存的的DbContext
和 DbSet
没有,但我的印象是,有一些高速缓存将继续下去。我看到的行为,当我运行下面的代码我不会期望:
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?
推荐答案
什么的是,当你使用 .Find
方法上的<$ EF只会检查缓存C $ C> DbSet 。
What @emcas88 is trying to say is that EF will only check the cache when you use the .Find
method on DbSet
.
使用。单
,。首先
,。凡
等不会,除非你正在使用的二级缓存缓存结果。
Using .Single
, .First
, .Where
, etc will not cache the results unless you are using second-level caching.
这篇关于为什么实体框架6.x中不缓存的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!