You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
publicstaticvoidAddMyInterceptor<TContext>(thisIServiceCollectionservices)whereTContext:DbContext{
services.TryAddScoped<MyInterceptor>();
services.AddDbContext<TContext>((serviceProvider,options)=>{ options.AddInterceptors(serviceProvider.GetRequiredService<MyInterceptor>());thrownew Exception("If this is thrown, it worked.");});}
The above implementation was just a guess, but given its usage:
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
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
The text was updated successfully, but these errors were encountered:
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.
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.
Ask a question
Is there a way to alter the configuration of a
DbContext
afterAddDbContext
has been called?My use case is that i want to provide an extension method, that hooks additional behavior via an
IInterceptor
, therefore:IServiceCollection
The above implementation was just a guess, but given its usage:
One could observe, that the optionsAction from the second call to
AddDbContext
inAddMyInterceptor
is silently ignored.Please correct me if I'm wrong at some point, but given that
IServiceCollection
fromDbContextOptions
DbContext
via a second call toAddDbContext
or another mechanismone would be left to implement such a "hook" by splitting it up into two methods
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
The text was updated successfully, but these errors were encountered: