Using the cache

We already talked about the first level (or session) cache. Basically, all records loaded from an Entity Framework context as the result of a query are added to this cache automatically. So, if we have loaded enough entities, we may have in this cache what we are looking for, without the need to go to the database.

Another kind of cache is sometimes called a second level cache. This kind of cache outlives a data context, meaning different instances of the same context share this cache. What is it good for? Well, it's good for reference data, for example, data that does not change so often. Once this data is loaded into memory, it can be made available to all contexts that need it, no need to query the database again, instantiate ...

Get Entity Framework Core Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.