Blazor ASP.NET Core 7.0
De Banane Atomic
Aller à la navigationAller à la recherche
Entity Framework Core
Using a DbContext factory
To handle multi-threads scenarios, use a AddDbContextFactory to create a DbContext for each query.
Program.cs |
builder.Services.AddDbContextFactory<MyDbContext>(); |
MyRepository.cs |
private readonly IDbContextFactory<MyDbContext> contextFactory; public DataSummaryRepository(IDbContextFactory<MyDbContext> contextFactory) { this.contextFactory = contextFactory; } public async Task<Data> FetchDataAsync() { // create a DbContext for this query, then dispose it once the query has been executed using var context = contextFactory.CreateDbContext(); } |