C
C#8mo ago
Spaxter

✅ EF Core long running query causing concurrency issues

Hello. I have a .NET application that uses a DbContext with PostgreSQL. To test the performance of my app, I added a million rows to one of the tables. I noticed that it takes a pretty long time to fetch all of these rows, and if I run the method to get all the rows again before the previous query finishes, it throws the following exception: System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. This is an example of how it is set up currently:
private MyDbContext _dbContext;

public DbService(MyDbContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}

// This method throws the excpetion if called again before the previous query is finished
public async Task<List<MyObj>> GetAllObjects()
{
return await _dbContext.MyTable.AsNoTracking().ToListAsync();
}
private MyDbContext _dbContext;

public DbService(MyDbContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}

// This method throws the excpetion if called again before the previous query is finished
public async Task<List<MyObj>> GetAllObjects()
{
return await _dbContext.MyTable.AsNoTracking().ToListAsync();
}
6 Replies
Saber
Saber8mo ago
As the error says, multiple operations cannot be preformed on the same context at the same time. You need separate instances to be able to have multiple queries running at the same time
Spaxter
Spaxter8mo ago
Right, but creating a new instance of the DbContext for every method call would be tough though wouldn't it?
Jimmacle
Jimmacle8mo ago
no, you can just inject IDbContextFactory<MyDbContext> and use that assuming you're using MSDI
Spaxter
Spaxter8mo ago
Oh that's a thing? Wow, how did I not know that Awesome, thanks!
Jimmacle
Jimmacle8mo ago
dbcontexts are supposed to be short lived, i almost always new up a fresh one for each operation or tightly related set of operations
Spaxter
Spaxter8mo ago
Sounds about right, I was about to write my own version of a DbContextFactory, guess I'm glad I asked here first
Want results from more Discord servers?
Add your server
More Posts
❔ (Beginner) Seeking Advice on Distributing Items Equally in C# ProgramHey everyone! I hope you're doing well. I'm still pretty new around here and not exactly a pro in t❔ ENV var dump collection not working on some machinesI'm using these env vars to collect dumps from my .net core macOS app (docs: https://learn.microsoft❔ How to play music files from resources/relative paths in wpf c# ?I have spend good 2 to 3 hours trying to get a sound file played from relative path, it just doesnt ❔ Simple code that I dont understand why it wont work.Hello, I am an IT specialized class student and we started learning C# in class, I am trying out somEmulating Windows FeaturesHello, I'm trying to figure out a couple of things before I start a new project. The first of which ❔ IEnumerable to ObservableCollectionI am not sure if im doing this correctly but I am trying to create a sqlite table with some data in ❔ EF Core optional where extension methodI have a lot of optional parameters for queries. Filter by this, filter by that etc. I can do it lik❔ Need help on UI Design and tips for implementation - .Net Mauithree questions: 1. How can I edit the blue bar on the top? I'm not sure where its coming from, I o❔ Implementation of Search page in ASP. NET MVC along with stored procedureCan anyone help me to create a webform to search the data from the database to filter it on web page❔ MySQL server issue (Error 28)I'll prefix this with a few things: - I'm not driven with Ubuntu or other unix based systems. - The