Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let AddSqlServer/AddSqlite easy to configure the options from provider #33813

Closed
WeihanLi opened this issue May 25, 2024 · 3 comments
Closed
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@WeihanLi
Copy link
Contributor

WeihanLi commented May 25, 2024

I want to resolve the interceptors from dependency injection using the minimal api, for now, the AddSqlServer/AddSqlite does not support configuring the DbContextOptionsBuilder from the service provider, maybe we could provide an extension for it as additions to #25192.

Currently, we have to do like this

builder.Services.AddDbContext<AppDbContext>((provider, options) =>
{
    options.AddInterceptors(provider.GetRequiredService<AutoUpdateInterceptor>());
    options.UseSqlServer(builder.Configuration.GetConnectionString("Default"));
});

with the new extension, we may simplify it like follows:

builder.Services.AddSqlServer<AppDbContext>(builder.Configuration.GetConnectionString("Default"), 
    _ => { }, (provider, options) => 
    {
        options.AddInterceptors(provider.GetRequiredService<AutoUpdateInterceptor>());
    });

something likes:

public static IServiceCollection AddSqlServer<TContext>(
    this IServiceCollection serviceCollection,
    string? connectionString,
    Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null,
    Action<DbContextOptionsBuilder>? optionsAction = null)
    where TContext : DbContext
{
    return serviceCollection.AddSqlServer<TContext>(connectionString, sqlServerOptionsAction, (_, options) => optionsAction?.Invoke(options));
}

public static IServiceCollection AddSqlServer<TContext>(
    this IServiceCollection serviceCollection,
    string? connectionString,
    Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null,
    Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction = null)
    where TContext : DbContext
{
    return serviceCollection.AddDbContext<TContext>((Action<IServiceProvider, DbContextOptionsBuilder>) ((provider, options) =>
    {
        optionsAction?.Invoke(provider, options);
        options.UseSqlServer(connectionString, sqlServerOptionsAction);
    }));
}
@ajcvickers
Copy link
Member

We will discuss, but, to me, the existing API seems like a good fit:

builder.Services.AddDbContext<AppDbContext>((provider, options) => options
    .AddInterceptors(provider.GetRequiredService<AutoUpdateInterceptor>())
    .UseSqlServer(builder.Configuration.GetConnectionString("Default")));

This should also work:

services.AddDbContext<AppDbContext>((provider, options) => options
    .AddInterceptors(provider.GetRequiredService<AutoUpdateInterceptor>())
    .UseSqlServer("name:Default"));

@WeihanLi
Copy link
Contributor Author

thanks

@ajcvickers
Copy link
Member

Discussed and team agreed not to make this change.

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. and removed type-enhancement needs-design labels Jun 4, 2024
@ajcvickers ajcvickers removed their assignment Jun 4, 2024
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants