• About Me
  • Contact Us
  • Privacy Policy
  • Terms and Conditions
  • Advertise / Sponsor

DOTNET DETAIL

Learn Microsoft .NET Technologies

  • Home
  • Tutorials
    • Angular
      • Angular 5
      • Angular 6
    • ASP.NET Core
    • Azure
    • React
    • Vue
  • Books
  • Courses
  • Cloud Hosting
  • Interview Questions
You are here: Home / Entity Framework / Entity Framework Core 3.0 Cache Busting | Dotnet Detail

Entity Framework Core 3.0 Cache Busting | Dotnet Detail

October 31, 2019 by mebakar1005 Leave a Comment

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. 

See also  Define one to many relationship in Entity Framework Code First

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!

Entity Framework Core 3.0 Bulk Insert Update and Delete - Asp.Net Core 3.0
How to integrate Vue and Asp.Net Core 3.1 app using VS Code

Related

Filed Under: Entity Framework Tagged With: Cache Busting, EF Core, ENTITY FRAMEWORK, Entity Framework Core

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Buy me a coffeeBuy me a coffee

Jobs

Dotnet Jobs

Join Us

Join Us

Subscribe to Blog via Email

Enter your email address to subscribe.

Recent Posts

  • Top LINQ interview questions and answers.
  • Difference between .NET Core and .NET Framework
  • Top 10 Programming Languages of the Future.
  • Top MVC Interview Questions & Answers
  • 20 best Data Science Books: Beginner to Advanced Level
  • 10 Best Machine Learning Laptops
Copyright © 2022