-
Notifications
You must be signed in to change notification settings - Fork 123
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
'OnConfiguring' cannot be used to modify DbContextOptions when DbContext pooling is enabled #1601
Comments
Have you tried using a key that matches your DbContext name? {
"Aspire": {
"Microsoft": {
"EntityFrameworkCore": {
"ApplicationDbContext": { <-- match DbContext name
"ConnectionString": "YOUR_CONNECTIONSTRING",
"DbContextPooling": false,
"DisableHealthChecks": true,
"DisableTracing": true,
"DisableMetrics": false
}
}
}
}
} |
Based on your suggestion, I started playing around with configuration using |
I've created a small repro repository: https://github.com/dradovic/repro-aspire-dbcontext Playing around with it, I've realized that:
So TL;DR: the question remains how to disable DbContext pooling. |
If you don't want to do pooling, you should be using the Enrich api https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.aspiresqlserverefcoresqlclientextensions.enrichsqlserverdbcontext?view=dotnet-aspire-8.0 - regardless we should put this in the docs somewhere |
Moving to docs but also we should add some XML info too |
@maddymontaquila Thanks for the suggestion. However, |
Enrich doesn't enable pooling, you are supposed to use the Enrich* method with the normal Entity Framework AddDbContext method (where you get full control over the configuration). i.e. Delete this: builder.AddSqlServerDbContext<ApplicationDbContext>("sqldb"); // this is new in order to... Replace it with: builder.Services.AddDbContext<ApplicationDbContext>(...);
builder.EnrichSqlServerDbContext<ApplicationDbContext>(); |
@davidfowl alright! Makes sense. So one uses the normal EF AddDbContext to configure everything as usual (including interceptors which would answer dotnet/aspire#3069) and then with |
Ok but the deleted line contained the database reference from the Aspire project (i.e. to auto-magically hook it up without a manually specified connection string), whereas the lines you have added - what do they look like with the ... expanded? Can I just throw the equivalent of "TestDB" in there somewhere as I could with AddSqlServerDbContext? What does the enrich part do to it? Is it intentional that AddSqlServerDbContext can't be used to migrate? |
From the docs Configures retries, health check, logging and telemetry for the DbContext.
AddSqlServerDbContext is more opinionated, that's why |
You can then use it like this: var postgres = builder.AddPostgres("Postgres");
var db = postgres.AddDatabase("DbName", "SomeDb"); Application: private static void AddDatabase(this WebApplicationBuilder builder)
{
var connectionString = builder.Configuration.GetConnectionString("DbName");
builder.Services.AddDbContextFactory<DbContext>(dbContext => dbContext.UseNpgsql(connectionString));
builder.EnrichNpgsqlDbContext<DbContext>(c => c.DisableTracing = true);
} |
Is there an existing issue for this?
Describe the bug
I'm trying to migrate a working ASP.NET Core app to Aspire .NET. I've added SQL Server support in the following way:
<PackageReference Include="Aspire.Hosting.SqlServer" Version="8.2.0" />
and a bit of configuration:<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="8.2.0" />
and I have:On the first usage (injection) of
ApplicationDbContext
, I get:The existing app never used the DbContext pooling feature, so I tried to disable it by adding:
to the AppHost's
appsettings.json
. However, the exception is still raised. Which makes me wonder if the"DbContextPooling": false
is respected. Or should I put this configuration somewhere else? (I've tried my luck into add it to the project'sappsettings
but that didn't change anything either.)Expected Behavior
With
"DbContextPooling": false
configured, I would expect that I don't get an error that relates to DbContext pooling.Steps To Reproduce
No response
Exceptions (if any)
System.InvalidOperationException
HResult=0x80131509
Message='OnConfiguring' cannot be used to modify DbContextOptions when DbContext pooling is enabled.
.NET Version info
.NET SDK:
Version: 8.0.401
Commit: 811edcc344
Workload version: 8.0.400-manifests.57f7c351
MSBuild version: 17.11.4+37eb419ad
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.401\
.NET workloads installed:
Configured to use loose manifests when installing new manifests.
[aspire]
Installation Source: SDK 8.0.400, VS 17.11.35219.272
Manifest Version: 8.2.0/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.0\WorkloadManifest.json
Install Type: Msi
Host:
Version: 8.0.8
Architecture: x64
Commit: 08338fcaa5
.NET SDKs installed:
5.0.408 [C:\Program Files\dotnet\sdk]
6.0.100 [C:\Program Files\dotnet\sdk]
6.0.133 [C:\Program Files\dotnet\sdk]
6.0.425 [C:\Program Files\dotnet\sdk]
7.0.120 [C:\Program Files\dotnet\sdk]
7.0.203 [C:\Program Files\dotnet\sdk]
7.0.410 [C:\Program Files\dotnet\sdk]
8.0.108 [C:\Program Files\dotnet\sdk]
8.0.400 [C:\Program Files\dotnet\sdk]
8.0.401 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
Anything else?
I'm developing using Visual Studio Professional 17.11.1.
The text was updated successfully, but these errors were encountered: