在实体框架CTP5中关闭对象缓存

时间:2021-07-12 02:15:53

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.

在ctp5中,我在用实体框架代码首先解决一些问题时遇到了麻烦。它对对象进行缓存,我不希望这样。例如,我加载一个页面(使用ASP。加载一个对象的netmvc站点。然后我去更改数据库。我重新加载页面,更改没有反映出来。如果我杀死网站并重新运行它,它显然会重新获取。对于一个类型,甚至是一个特定的查询,我如何告诉它总是去获取一个新的副本。我认为这可能与MergeOption有关,但是我很难找到使用CTP 5的例子。谢谢。

1 个解决方案

#1


15  

Okay, figured it out. The following will sometimes pull from the EF cache:

好吧,算出来。以下内容有时会从EF缓存中提取:

return (from m in _dataContext.Monkeys
        where m.MonkeyId == monkeyId
        select m).FirstOrDefault();

You can use AsNoTracking() to bypass the change tracking/caching stuff:

您可以使用AsNoTracking()绕过更改跟踪/缓存内容:

return (from m in _dataContext.Monkeys.AsNoTracking()
        where m.MonkeyId == monkeyId
        select m).FirstOrDefault();

#1


15  

Okay, figured it out. The following will sometimes pull from the EF cache:

好吧,算出来。以下内容有时会从EF缓存中提取:

return (from m in _dataContext.Monkeys
        where m.MonkeyId == monkeyId
        select m).FirstOrDefault();

You can use AsNoTracking() to bypass the change tracking/caching stuff:

您可以使用AsNoTracking()绕过更改跟踪/缓存内容:

return (from m in _dataContext.Monkeys.AsNoTracking()
        where m.MonkeyId == monkeyId
        select m).FirstOrDefault();

相关文章