简体   繁体   中英

Parallel Query Execution In Entity Framework Core

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM