How to refresh an Entity Framework Core DBContext?
When my table is updated by another party, the db context in dotnet core still return the old value, how can I force the Db context to refresh?
I've done research but I only found people use Reload
method, which is not available in EF core, to force the context to refresh.
Some other solution suggests dispose the context after using, but I get error saying the DB context is created by dependency injection and I should not mess up with it.
Oh, this issue had me in knots for days.
I'm using Visual Studio 2017 with .Net Core 2.1, and my EF Core code looked something like this:
// 1. Load a [User] record from our database
int chosenUserID = 12345;
User usr = dbContext.Users.FirstOrDefault(s => s.UserID == chosenUserID);
// 2. Call a web service, which updates that [User] record
HttpClient client = new HttpClient()
await client.PostAsync("http://someUrl", someContent);
// 3. Attempt to load an updated copy of the [User] record
User updatedUser = dbContext.Users.FirstOrDefault(s => s.UserID == chosenUserID);
At step 3, it would simply set "updatedUser" to the original version of the [User] record, rather than attempting to load in a fresh copy. So, if, after step 3, I modified that [User] record, I'd actually lose whatever settings the web service had applied to it.
I - eventually - found two solutions.
I could change the ChangeTracker
settings. This worked, but I was concerned about the side-effects of doing this:
dbContext.ChangeTracker.QueryTrackingBehavior = Microsoft.EntityFrameworkCore.QueryTrackingBehavior.NoTracking;
Or, I could slip in the following command, before attempting to reload the [User] record...
await dbContext.Entry(usr).ReloadAsync();
This seems to force .Net Core to reload that [User] record, and life is good again.
I hope this is useful...
Tracking down, and fixing this bug took me days....
There's also an excellent article describing the various ways to get around this caching issue here.
Dependency Injection and DbContext
You mention that when you try to recreate your DbContext
, you get an error about the context being managed by your dependency injection (DI) system. There are two different styles of using a dependency injection system for object creation. The DI can either create a global singleton instance that is shared as a service between all consumers or it can create an instance per scope/unit of work (e.g., per request in a web server).
If your DI system is configured to create a single global shared instance of DbContext
, then you will encounter various problems associated with long-lived DbContext
.
-
DbContext
, by design, never automatically removes objects from its cache because it is not designed to be long-lived. Thus, a long-livedDbContext
will retain memory wastefully. - Your code will never see changes to items loaded into its cache without manually reloading each entity it loads.
-
DbContext
only allows one query to run at any time and is not threadsafe. If you try to run multiple queries on a globally shared instance, it will throwDbConcurrencyException
(at least on its async interface, not sure about its sync interface).
Thus, the best practice is to use a single DbContext
per unit of work. Your DI system can help you with this by being configured to provide a fresh instance for each request your application processes within a scope. For example, ASP.NET Core’s Dependency Injection system supports scoping instances by request.
Refreshing a Single Entity
The easiest way to get fresh data is to create a new DbContext
. However, within your unit of work, or within the constraints of the granularity of scoping provided by your DI system, you may trigger an external process which is supposed to modify your entity directly in the database. You may need to see that change before exiting your DI’s scope or completing your unit of work. In that case, you can force a reload by detaching your instance of the data object.
To do this, first get the EntityEntry<>
for your object. This is an object which lets you manipulate DbContext
’s internal cache for that object. You can then mark this entry detached by assigning EntitytState.Detached
to its State
property. I believe that this leaves the entry in the cache but causes the DbContext
to remove and replace it when you actually load the entry in the future. What matters is that it causes a future load to return a freshly loaded entity instance to your code. For example:
var thing = context.Things.Find(id);
if (thing.ShouldBeSentToService) {
TriggerExternalServiceAndWait(id);
// Detach the object to remove it from context’s cache.
context.Entities(thing).State = EntityState.Detached;
// Then load it. We will get a new object with data
// freshly loaded from the database.
thing = context.Things.Find(id);
}
UseSomeOtherData(thing.DataWhichWasUpdated);
Reload
and ReloadAsync
has been available since Entity Framework Core 1.1
Examples:
//test.Name is test1
var test = dbContext.Tests.FirstOrDefault();
test.Name = "test2";
//test.Name is now test1 again
dbContext.Entry(test).Reload();
https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.changetracking.entityentry.reload?view=efcore-1.1
With the release of .NET 5.0 and Entity Framework Core 5.0, the recommended pattern would be to use a DBContext factory. In Statup.cs I changed:
services.AddDbContext<MyDbContext>...
to
services.AddDbContextFactory<MyDbContext>...
All my repository classes use the same base class, here I create the context in the constructor:
protected BaseRepository(IDbContextFactory<MyDbContext> contextFactory)
{
_context = contextFactory.CreateDbContext();
}
I use a very simple repository factory to ensure I get a fresh instance of the repository and dbcontext each time I need it:
using System;
using Data.Models.Entities;
using Microsoft.Extensions.DependencyInjection;
namespace Data.Repository
{
public class RepositoryFactory : IRepositoryFactory
{
private readonly IServiceProvider _serviceProvider;
public RepositoryFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public IApplicationRepository BuildApplicationRepository()
{
var service = _serviceProvider.GetService<IApplicationRepository>();
return service;
}
}
}
Using the patterns described solves the "DB context is created by dependency injection" error and negates the need for a Reload()/Refresh() method.