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

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

问题描述

也许我误解了$ code> 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?

推荐答案

什么 @ emcas88试图说是,当您使用 .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.

使用 .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天全站免登陆