How does EFCache detach entities

Dec 6, 2014 at 7:29 PM
Edited Dec 6, 2014 at 7:36 PM
Hi All,

I'm using EFCache and it's working very well. There is some data however that I want to perform extra caching on (pre load the data and use it throughout my app) but I keeping running into issues with my cached entities still being referenced by a long gone context.

I notice that EFCache doesn't have this issue so how does it do it? I've looked through the code and can't see any references to detaching / attaching so would appreciate some input.

p.s I know this isn't strictly to do with EFCache but I'd appreciate your thoughts nonetheless.
Coordinator
Dec 7, 2014 at 7:40 AM
Hi webnoob,

EFCache works at much lower level and does not cache entities but caches results returned by the database. When EF sends a SQL query to the database EFCache checks if the result for this query is cached and if it is it returns its own implementation of the DataReader containing cached results instead of sending the query to the database.
I am not sure what "cached entities still being referenced by a long gone context" means - if you Dispose context then how it can reference entities? Note that entities don't have a back reference to context. I think that if you have a set of entities that don't change and you want to cache them you would attach them each time you create a new instance of DbContext. If you don't change them then they will never be saved to the database. It's hard to tell more without more details about issues what you are running into (it feels however that asking a question on stackoverflow might yield better results if this is more general than just EFCache).

Thanks,
Pawel
Dec 7, 2014 at 10:52 AM
Edited Dec 7, 2014 at 10:53 AM
Hi Pawel,

Really appreciate your time in responding to this. I've changed how things work a little bit so the issue is now sorted, I just wanted to understand how EFCache works in this regard because it obviously doesn't suffer from the same issues (which I can understand why) so thanks again for the response.

p.s This post on SO explains my issue a little clearer: http://stackoverflow.com/questions/27335291/how-to-detach-entity-from-context-when-you-no-longer-have-a-context-reference

I've decided to re-load items from the DB prior to editing them so browsing (general reads) is fast but when something needs attaching to an existing entity, I just load it based on Id.

Regards,
webnoob
Coordinator
Dec 9, 2014 at 5:29 PM
Here is the idea - if you preload entities from the DB I assume they don't change as much. As a result you could use EFCache for the entity set they belong too so that they are cached. Now you should have the best of two worlds - you would just preload them as you do and avoid any tricks with attaching/detaching but you would not have to pay for the trip to the database because the data would be read from the cache (after the first query) and not from the database.

Thanks,
Pawel
Dec 9, 2014 at 8:03 PM
Hi Pawel,

That is exactly what I decided to do. I'm really impressed with how EFcache works so its great in general and works well alongside my pre-caching.

Sorry, should have updated sooner.

Regards,
webnoob
Coordinator
Dec 11, 2014 at 6:18 AM
Thanks for the kind words. I am glad you found this project helpful.

Thanks,
Pawel