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

How to add SaveChanges interceptors when using AddSqlServerDbContext #2246

Open
dedalusmax opened this issue Dec 9, 2024 · 8 comments
Open
Labels
⌚ Not Triaged Not triaged

Comments

@dedalusmax
Copy link

dedalusmax commented Dec 9, 2024

Discussed in dotnet/aspire#4202

Originally posted by jstynder May 16, 2024
When adding interceptors this would be the way I do It now:

  services.AddDbContextPool<T>((serviceProvider, optionsBuilder) =>
      optionsBuilder
          .UseSqlServer(connectionString)
          .AddInterceptors(serviceProvider.GetRequiredService<SecondLevelCacheInterceptor>()));

How can I access the serviceProvider when using AddSqlServerDbContext

@jack775544
Copy link

You should be able to use the EnrichSqlServerDbContext method to achieve what you want. The docs for this approach are here: https://learn.microsoft.com/en-us/dotnet/aspire/database/sql-server-entity-framework-integration?tabs=dotnet-cli%2Cssms#add-sql-server-database-context-with-enrichment

This way you can add the context using the AddDbContextPool method like you showed above and the enrich method will add all the extra things like telemetry and health checks.

@DamianEdwards
Copy link
Member

Yep the approach @jack775544 suggests is what we intend here. You need to use the regular AddDbContextPool method and then call EnrichSqlServerDbContext to add the Aspire goodness.

@dedalusmax
Copy link
Author

Yes, but this means we cannot use a containerized SQL database, but rather a real one - with the extra things that Aspire provides. This is what bothers me (or probably other guys) - I want to use AddResource like a containerized resource, and on top of that, use everything that is necessary to configure a DB with EF Core (like adding interceptors alike).

p.s. I find a general problem that all the methods for configuring the integrations are made as extensions to the IHostBuilder - not IServiceCollection - which doesn't fit in all scenarios (like I'm building now with a console app).

@davidfowl
Copy link
Member

Yes, but this means we cannot use a containerized SQL database, but rather a real one

What does this mean? How does using the enrich method change this?

@dedalusmax
Copy link
Author

dedalusmax commented Dec 10, 2024

Sorry if I'm not clear in describing the problem. For clarity, just refer to the original question/problem that is asked.
This is what is currently possible/intended with Aspire's configuration:

builder.AddSqlServerDbContext<ApplicationDbContext>(connectionName: "sql-db", configureDbContextOptions: options =>
{
    options.AddInterceptors(); // no way to correctly configure it!!!
});

this is what is needed:

builder.AddSqlServerDbContext<ApplicationDbContext>(connectionName: "sql-db", (serviceProvider, options) =>
{
    var interceptor = serviceProvider.GetService<AuditableEntitiesInterceptor>()!;
    options.UseSqlServer(connectionString).AddInterceptors(interceptor);
});

P.S. Enrich method won't help, as it takes MicrosoftEntityFrameworkCoreSqlServerSettings type as a parameter:
builder.EnrichSqlServerDbContext(settings => { });

@davidfowl
Copy link
Member

davidfowl commented Dec 10, 2024

You are supposed to use the enrich method with the normal AddDbContext method.

builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("sql-db"));
    options.AddInterceptors(..);
});

builder.EnrichSqlServerDbContext<ApplicationDbContext>();

Maybe we need to improve our docs here @IEvangelist

@dedalusmax
Copy link
Author

that worked! thank you for the explanation.
yes, i would say it's rather confusing and not clarified completely.

kind regards!

@dotnetrepoman dotnetrepoman bot added the ⌚ Not Triaged Not triaged label Dec 10, 2024
@joperezr joperezr transferred this issue from dotnet/aspire Dec 10, 2024
@joperezr
Copy link
Member

Transferred to dotnet/docs-aspire repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⌚ Not Triaged Not triaged
Projects
None yet
Development

No branches or pull requests

5 participants