Reputation: 2602
I use blazor server side and register my DbContext as transient also I register the data access class as transient, But if parameters are not changed EF Core always returns cached data Unless there is an Update before Query, then it returns fresh data.
I tried to create an instance of DbContext every time each data access method is called instead of Dependency injection but it also returns cached data.
services.AddDbContext<StoreContext>(options =>
options.UseSqlServer(
Configuration["Data:StoreProducts:ConnectionString"]), ServiceLifetime.Transient);
services.AddTransient<DataAccess.Dal>();
This is dal:
public UserLock GetByUserName(string userName)
{
return context.UserLocks.FirstOrDefault(l => l.User.ToLower() == userName.ToLower());
}
or:
public UserLock GetByUserName(string userName)
{
using (var db = new StoreContext())
{
return context.UserLocks.FirstOrDefault(l => l.User.ToLower() == userName.ToLower());
}
}
How to solve this problem?
Upvotes: 1
Views: 767
Reputation: 456
If you are just looking to get the user and returning it, you can add the AsNoTracking() in your linq query to prevent EF from using what is cached.
Upvotes: 1
Reputation: 2602
I solved my problem like this:
public UserLock GetByUserName(string userName)
{
UserLock ulock = context.UserLocks.FirstOrDefault(l => l.User.ToLower() == userName.ToLower());
context.Entry(ulock).Reload();
return ulock;
}
Upvotes: 3