开发者

Caching an EF entity in ASP.NET cache

I have an MVC3 site with EF4.1 and repository/unit of work pattern and creating a new context per http request.

I want to implement custom object caching in Entity Framework 4.1. This means adding an object to the asp.net cache for faster retrieval by other similar http requests.

But the problem with EF is that these cached entities are attached to the context that was used to retrieve this entity in the first place and that got disposed after that http request was over.

Now wh开发者_JS百科en i want to update this cached object it gives me "An entity object cannot be referenceed by multiple instances of IEntityChangeTracker" Which makes complete sense.

What is the best practice to implement object caching in MVC3/EF4.1?

I am guessing you have to detach the entity from the old context while adding to the cache and attaching to the new context while retrieval. But i am surely not the first one to encounter this or try to implement this!

Are there any helpers or libraries out there? (please dont point me to http://code.msdn.microsoft.com/EFProviderWrappers/Release/ProjectReleases.aspx?ReleaseId=4747 as this removes ALL entries from cache on any update to a table, proving itself useless).

Thank you.


"Best practice" would probably be to use a special DTO class to store on the session, and map to and from that class when you need to save or load information in the database.

However, you should be able to achieve what you're going for just by detaching and attaching the entity like you say. It shouldn't be all that difficult if you consolidate your logic for adding and removing it from the session into a single class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜