dotnet / aspire

An opinionated, cloud ready stack for building observable, production ready, distributed applications in .NET
https://learn.microsoft.com/dotnet/aspire
MIT License
3.37k stars 348 forks source link

Provide `AddSqlServerDbContextFactory` method for `Aspire.Microsoft.EntityFrameworkCore.SqlServer` #4296

Open uabarahona opened 1 month ago

uabarahona commented 1 month ago

The recommendation for Blazor web applications is to use AddDBContextFactory to handle the lifetime of the DBContext, I believe Aspire.Microsoft.EntityFrameworkCore.SqlServer should also provide that method to support that recommendation.

PD: Amazing tech

mitchdenny commented 4 weeks ago

@eerhardt PTAL

eerhardt commented 4 weeks ago

You can use any EF API to add your DbContext as normal, and then use the EnrichXDbContext method to add the Aspire specific capabilities to it.

Take a look at

You might also need to configure specific option of Sql Server, or register a DbContext in other ways. In this case call the EnrichSqlServerDbContext extension method, for example:

var connectionString = builder.Configuration.GetConnectionString("catalogdb");
builder.Services.AddDbContextPool<CatalogDbContext>(dbContextOptionsBuilder => dbContextOptionsBuilder.UseSqlServer(connectionString));
builder.EnrichSqlServerDbContext<CatalogDbContext>();

@sebastienros @IEvangelist - I don't see any information about the Enrich APIs at https://learn.microsoft.com/en-us/dotnet/aspire/database/sql-server-entity-framework-component. Should we add something there?