实体框架4和查询结果的缓存 [英] Entity Framework 4 and caching of query results

查看:98
本文介绍了实体框架4和查询结果的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有包含将永远不会或很少变化数据表或2,有没有试图缓存这些数据的任何一点?或将EF上下文缓存,对我的数据当我加载它们的第一次?我想从装载这些表中的所有数据,并使用一个静态列表或东西保存在内存中的数据和查询内存中的数据,而不是表,每当我需要在同一范围内的数据。我说这些表中包含的数据通常只有几百行。

Say I have a table or 2 that contains data that will never or rarely change, is there any point of trying to cache those data? Or will the EF context cache that data for me when I load them the first time? I was thinking of loading all the data from those tables and use a static list or something to keep that data in memory and query the in memory data instead of the tables whenever I need the data within the same context. Those tables I speak of contains typically a few hundred rows of data.

推荐答案

的EF上下文将缓存每个实例。即,对象的的DbContext 保持它自己的独立的高速缓存的每个实例。可以将得到的对象的列表存储在一个静态列表和查询它所有你喜欢不返回到数据库。为了安全起见,一定要放弃的DbContext 之后的执行查询。

The EF context will cache "per instance". That is, each instance of the DbContext keeps it's own independent cache of objects. You can store the resulting list of objects in a static list and query it all you like without returning to the database. To be safe, make sure you abandon the DbContext after you execute the query.

var dbContext = new YourDbContext();
StaticData.CachedListOfThings = dbContext.ListOfThings.ToList();

您以后可以使用LINQ查询静态列表。

You can later use LINQ to query the static list.

var widgets = StaticData.CachedListOfThing.Where(thing => thing.Widget == "Foo");



查询执行内存中的集合,而不是数据库。

The query executes the in-memory collection, not the database.

这篇关于实体框架4和查询结果的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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