-
Notifications
You must be signed in to change notification settings - Fork 115
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
Comments
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. |
Yep the approach @jack775544 suggests is what we intend here. You need to use the regular |
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). |
What does this mean? How does using the enrich method change this? |
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 needed:
P.S. Enrich method won't help, as it takes MicrosoftEntityFrameworkCoreSqlServerSettings type as a parameter: |
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 |
that worked! thank you for the explanation. kind regards! |
Transferred to dotnet/docs-aspire repo. |
Discussed in dotnet/aspire#4202
Originally posted by jstynder May 16, 2024
When adding interceptors this would be the way I do It now:
How can I access the
serviceProvider
when usingAddSqlServerDbContext
The text was updated successfully, but these errors were encountered: