如何使Entity Framework缓存一些对象 [英] How to make Entity Framework cache some objects

查看:83
本文介绍了如何使Entity Framework缓存一些对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架。



在我的数据库中存储人员和国家的起源。当我加载一个人的对象时,我不知道每个人的起源是什么。有没有办法缓存国家列表,以免不必要的请求。

解决方案

正如卢曼已经说过我的伙伴Jarek为实体框架提供了一个很好的示例缓存提供程序。他谈论的是这里



但是有时这种方法是过度的,我怀疑缓存国家是一个很好的例子,不值得。实现某种特殊缓存可能会更好。



如果您尝试这样做,您将遇到的主要问题是实例实体框架实体只能一次连接到一个ObjectContext,这意味着如果您获得并发请求(如在网站中),您将遇到问题,您只需从缓存中抓取项目并盲目使用。



您需要的是克隆缓存,即当您从缓存中抓取某些东西克隆它们时。



我将一个提示说明如何执行此操作提示14 - 如何缓存实体框架参考数据



希望这有助于



Alex James

实体框架团队Microsoft。


I'm using Entity Framework.

In my database are stored persons and country of their origins. When I'm loading a person object I wan't to know what is the place each person originates. Is there a way to cache list of countries so that there wouldn't be unnecessary requests.

解决方案

As Luhmann has already said my mate Jarek has an excellent sample Caching provider for the Entity Framework. Which he talks about here.

However sometimes this approach is overkill, and I suspect that caching countries is a good example where the effort isn't worth it. It maybe better to implement some sort of ad-hoc caching.

If you try to do this the main problem you will run into is that an instance Entity Framework entity can only be attached to one ObjectContext at a time, which means if you are getting concurrent requests (like in a website) you will have problems is you just grab items from the cache and use them blindly.

What you need is a cloning cache, i.e. so when you grab something from the cache it clones them.

I put together a tip that illustrates how to do this Tip 14 - How to cache Entity Framework Reference Data

Hope this helps

Alex James

Entity Framework Team Microsoft.

这篇关于如何使Entity Framework缓存一些对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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