Blazor ASP.NET Core 7.0

De Banane Atomic
Version datée du 28 mars 2023 à 09:29 par Nicolas (discussion | contributions) (Page créée avec « Category:Blazor = [https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/#using-a-dbcontext-factory-eg-for-blazor Entity Framework Core] = To handle multi-threads scenarios, use a {{boxx|AddDbContextFactory}} to create a DbContext for each query. <filebox fn='Program.cs'> builder.Services.AddDbContextFactory<MyDbContext>(); </filebox> <filebox fn='MyRepository.cs'> private readonly IDbContextFactory<MyDbContext> contextFactory; public DataSumma... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigationAller à la recherche

Entity Framework Core

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