In this tutorial, we are going to cover different kinds of entity framework core cache busting techniques with simple examples. Actually, when we run a query in EF Core, the DbContext in it automatically caches the data that it retrieves from the database using query. In some cases it is helpful because you don’t need to hit database in every request. But there are some cases in which data changes outside the context and then in these cases the query retrieves stale data. Now, the question is how can we handle this situation using entity framework core? So, we will see how to bust cache in entity framework ( EF ) Core.
In one of the previous tutorials, we have discussed about Entity Framework Core 3.0 bulk insert, update and delete operation.
Entity Framework Core Cache Busting
There are different kinds of techniques to bust cache in entity framework core. Let’s discuss one by one with simple examples.
1- Disable Tracking
In this technique, we will see how to disable tracking using AsNoTracking(). AsNoTracking() is a powerful method. It returns a new query and the returned entities will not be cached by the DbContext. It means we can use this method ( AsNoTracking() ) in read-only scenario. So, entity framework doesn’t perform any additional processing on entities like we cannot update these entities without attaching to the context. So, let’s see how to use AsNoTracking() in the query.
2- Refresh Entities
In this technique, we will see how to update entities in entity framework core cache using Refresh() or Reload() methods. So, if you have a small amount of entities to refresh or one entity to refresh then you can do it using below code.
Let’s understand the above code.
In line # 1, we are getting list of employees in empList variable.
In line # 4, we are reloading every entity and it will hit the database for each entity reload().
So, we cannot use the above code for large collection of entities to reload. Because it will hit the database for each and every entity that we reload.
So, if you have a large collection of entities to refresh, then we can do it using below code.
So, the above code will refresh all the entities in bulk. So, it will not hit the database for each and every entity.
3- Detach the entities
In this technique, we will see how to detach entities using Detached() method in entity framework core. In this way, we can remove the entities from cache using detach method. So, let’s see how to do it using below code.
Let’s understand the above code.
In line # 1, we are getting specific record from cache.
In line # 2, we are detaching the entity from the cache.
4- Using GetDatabaseValues() Method
In this technique, we will see how to use GetDatabaseValues() method using entity framework core. So, when we use GetDatabaseValues() method, it bypass the context cache. It means the returned object using GetDatabaseValues() method will not affect the cache and the context will not track it. It will query to database for the latest data for a particular entity. Let’s see how to use it in entity framework core.
Thank you for reading. Please keep visiting and sharing this blog within your community. Thank You!
Leave a Reply