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

Add ServiceCollection extension method for removing a DbContext or its options #33571

Closed
michaeltg17 opened this issue Apr 18, 2024 · 2 comments
Labels
area-dbcontext closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@michaeltg17
Copy link

When creating integration tests for a project with ASP.NET Core, you probably want to modify the already registered DbContext with your options for testing. So for example, you may want to change the connection string or enable sensitve data logging.

To do so you you cannot just call again AddDbContext with your new options, as they are registered with TryAdd. You have to mess with serviceDescriptors and remove the options for that DbContext (once you know that removing the descriptor for DbContext is not what you need).

Code sample:

        public static IServiceCollection RemoveDbContextOptions<TContext>(this IServiceCollection services) where TContext : DbContext
        {
            var serviceDescriptor = services.Single(d => d.ServiceType == typeof(DbContextOptions<AppDbContext>));
            services.Remove(serviceDescriptor);
            return services;
        }
                services.RemoveDbContextOptions<AppDbContext>();
                services.AddDbContext<AppDbContext>(options => options.EnableSensitiveDataLogging());

An extension method like the previous one could help in this case for removing DbContexts or its options when needed.

@michaeltg17 michaeltg17 changed the title Add ServiceCollection extension method for removing a DdContext or its options Add ServiceCollection extension method for removing a DbContext or its options Apr 18, 2024
@ajcvickers
Copy link
Contributor

@AndriySvyryd Is there any connection between this and the config changes you made for Aspire? I'm inclined to backlog this unless you think otherwise.

@AndriySvyryd
Copy link
Member

Yes, #32518 adds ConfigureDbContext that is meant for this scenario, it will override any existing conflicting configuration.

@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 22, 2024
@AndriySvyryd AndriySvyryd modified the milestones: 9.0.0-preview4, 9.0.0-preview1 Apr 22, 2024
@roji roji modified the milestones: 9.0.0-preview1, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dbcontext closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

No branches or pull requests

4 participants