Reputation: 254
Since EF Core can't handle parallel queries at the same time on the same context instance as documentation says, is there any common pattern that we can achieve this in .NET Core 6 using dependency injection?
Given the condition that I have no write and all of them are .AsNoTracking
in this scope.
Upvotes: 3
Views: 1451
Reputation: 51715
Quoting MS Documentation:
The recommended approach to create a new DbContext with dependencies is to use a factory.
builder
.Services
.AddDbContextFactory<ContactContext>(opt =>
opt
.UseSqlite($"Data Source={nameof(ContactContext.ContactsDb)}.db"));
You can get the factory on constructor via DI as usual:
IDbContextFactory<ContactContext> DbFactory
And create as many contexts as you need, don't forgot to dispose contexts.
using var context = DbFactory.CreateDbContext();
Upvotes: 3