Reputation: 177
I am developing a WebAPI on .NETCore accessing data to a POSTGRESQL DB. I have troubles with the non-MARS support of PostgreSQL. NPGSQL is unable to support multiple connections from the same instance (as described in EntityFramework DbContext lifecycle + Postgres: "An operation is already in progress."). For Asynchronous management, this is blocking.
Unfortunately, I cannot find any solution to this. At the moment, I inject my DB context with:
services.AddEntityFrameworkNpgsql()
.AddDbContextPool<DBApiContext>(
opt => opt.UseNpgsql('connectionString'));
I use EntityFramework.
Upvotes: 4
Views: 14200
Reputation: 1888
A command is already in progress
Experienced this exception and for my case I was using ToAsyncEnumerable()
in my select query and then trying to use the returned objects in another select database query.
ToAsyncEnumerable() will execute the database call when you are using the object (lazy loaded)
Upvotes: 1
Reputation: 315
Just for people who got stuck in this - In my case, it was a simple code change in the end of the method to fix this:
reader.close();
reader is an object of NpgsqlDataReader.
Upvotes: 19
Reputation: 177
For who may be interested in: my problem was all about service scope and dependency injection. The requestor was not a transient service, so that for every requests, even parallel, it was trying to access the DB. Postgresql doesn't support MARS, then the second requests were rejected.
You need to have transient service requesting the access, for every invokation to use a different DB handler.
Upvotes: 5