We are using EF code first with the App Fabric cache on Windows Azure (although, I think the question is really more generic since we are using it as an ASP.net caching provider). Is there an easy way to enable caching of DBset objects? Our db is small and not updated very frequently, so ideally we could cache the entire database in memory, and use some ttl expiry to refresh object sets. Any advise from someone with experience caching using EF code first would be great.
Asked
Active
Viewed 1,587 times
0

Ladislav Mrnka
- 360,892
- 59
- 660
- 670

Birdman
- 869
- 2
- 10
- 14
-
I believe EF actually caches results automatically. – BlueRaja - Danny Pflughoeft Feb 16 '12 at 19:44
2 Answers
3
Don't do that. If you want to cache data, extract them to separate Lists and cache them separately. Caching DbSet
means caching DbContext
which I would promote to anity-pattern in Entity framework. Problems with identity map and unit of work are described in linked answer. Another problem is that there is no real refresh. If you really want to refresh data you must dispose context and create a new one. Context is also not thread safe so sharing it among multiple requests can cause unexpected results.

Community
- 1
- 1

Ladislav Mrnka
- 360,892
- 59
- 660
- 670