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

Is there a way to alter the DbContext configuration after AddDbContext has been called? #31905

Closed
peterwurzinger opened this issue Sep 29, 2023 · 5 comments

Comments

@peterwurzinger
Copy link

peterwurzinger commented Sep 29, 2023

Ask a question

Is there a way to alter the configuration of a DbContext after AddDbContext has been called?

My use case is that i want to provide an extension method, that hooks additional behavior via an IInterceptor, therefore:

  • Adds the required services to the IServiceCollection
  • Adds the interceptor to a given context
public static void AddMyInterceptor<TContext>(this IServiceCollection services)
  where TContext : DbContext
{
  services.TryAddScoped<MyInterceptor>();
  services.AddDbContext<TContext>((serviceProvider, options) =>
  {
    options.AddInterceptors(serviceProvider.GetRequiredService<MyInterceptor>());
    throw new Exception("If this is thrown, it worked.");
  });
}

The above implementation was just a guess, but given its usage:

services.AddDbContext<MyContext>(o=>
{
  o.UseSqlServer(....)
}).AddMyInterceptor<MyContext>();

One could observe, that the optionsAction from the second call to AddDbContext in AddMyInterceptor is silently ignored.

Please correct me if I'm wrong at some point, but given that

  • There is no way to resolve an underlying IServiceCollection from DbContextOptions
  • There is no way to alter the configuration of an already configured DbContext via a second call to AddDbContext or another mechanism
    one would be left to implement such a "hook" by splitting it up into two methods
services.AddMyInterceptorServices();
//...
dbOptions.AddMyInterceptor(serviceProvider);

But that feels unnecessarily error-prone, but I might be biased.

Include provider and version information

EF Core version: 7.0.11
Database provider: I would guess it doesn't matter, but Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Win 11
IDE: VS 2022 17.6.5

@bachratyg
Copy link

I believe this covers what you need: #30061. There is no simple solution unfortunately.

@peterwurzinger
Copy link
Author

Indeed, thanks for pointing there.

Just for the sake of documenting the state as of September 2023: (EFCore 7):
There currently is no way to do that, and the proposed solution is

services.AddMyInterceptorService()
dbOptions.UseMyInterceptor(...)

@ajcvickers
Copy link
Member

Is there a way to alter the configuration of a DbContext after AddDbContext has been called?

You can override OnConfiguring on the DbContext. OnConfiguring is still called, even if AddDbContext is used. Typically, this call can use state from the context instance or the service provider as needed.

@peterwurzinger
Copy link
Author

Yes obviously, I missed to point that as an option. But what I'm looking for is an opt-in/plug-in solution, where the targeted DbContext doesn't (need to) know about the feature it is augmented with.

@ajcvickers
Copy link
Member

Duplicate of #30061

@ajcvickers ajcvickers marked this as a duplicate of #30061 Oct 2, 2023
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants