在实体框架CTP5中关闭对象缓存 [英] Turn Off Object Caching in Entity Framework CTP5
问题描述
我在使用CTP 5中的Entity Framework Code First东西搞清楚时遇到了麻烦.它正在缓存对象,但我不希望这样做.例如,我加载一个页面(使用ASP.NET MVC站点),该页面加载一个对象.然后,我去更改数据库.我重新加载页面,更改没有反映出来.如果我杀死该站点并重新运行,则显然会重新获取.通常,对于一个类型,甚至对于一个特定的查询,我该如何告诉它始终获取新副本.我认为这可能与MergeOption有关,但是我在查找与CTP 5兼容的示例时遇到了麻烦.
I am having trouble figuring out something with the Entity Framework Code First stuff in CTP 5. It is doing caching of objects and I don't want it to. For example, I load a page (working with an ASP.NET MVC site) which loads an object. I then go change the database. I re-load the page and the changes are not reflected. If I kill the site and rerun it then it obviously re-fetches. How do I, either generally for a type, or even for a particular query, tell it to always go get a new copy. I think it might have something to do with MergeOption but I'm having trouble finding examples that work with CTP 5. Thanks.
推荐答案
好,知道了.以下有时会从EF缓存中提取:
Okay, figured it out. The following will sometimes pull from the EF cache:
return (from m in _dataContext.Monkeys
where m.MonkeyId == monkeyId
select m).FirstOrDefault();
您可以使用AsNoTracking()绕过更改跟踪/缓存内容:
You can use AsNoTracking() to bypass the change tracking/caching stuff:
return (from m in _dataContext.Monkeys.AsNoTracking()
where m.MonkeyId == monkeyId
select m).FirstOrDefault();
这篇关于在实体框架CTP5中关闭对象缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!