« Blazor ASP.NET Core 7.0 » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
[[Category:Blazor]]
[[Category:Blazor]]
= [https://learn.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-7.0 Entity Framework Core] =
= [https://learn.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-7.0 Entity Framework Core] =
== [https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/#using-a-dbcontext-factory-eg-for-blazor Using a DbContext factory] ==
To handle multi-threads scenarios, use a {{boxx|AddDbContextFactory}} to create a DbContext for each query.
To handle multi-threads scenarios, use a {{boxx|AddDbContextFactory}} to create a DbContext for each query.


Ligne 21 : Ligne 22 :
}
}
</filebox>
</filebox>
* [https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/#using-a-dbcontext-factory-eg-for-blazor Using a DbContext factory]

Version du 28 mars 2023 à 09:35

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