`DatabaseDeveloperPageExceptionFilter` produces bizzare exception if model fails to initialize
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
`DatabaseDeveloperPageExceptionFilter` attempts to get `IModel`s from all registered `DbContextOptions`:
https://github.com/dotnet/aspnetcore/blob/d301328737c4912b95f236c4b349cf46ef7a473a/src/Middleware/Diagnostics.EntityFrameworkCore/src/DatabaseDeveloperPageExceptionFilter.cs#L58-L69
In our case, some of the registered `DbContext`s are expected to be used under very specific circumstances and throw an exception otherwise. Since `GetContextDetailsAsync` is not guarded against exceptions in any way, the "final" exception is the `DbContext` initialization failure, not the original exception, hindering debugging.
### Expected Behavior
Original exception is clearly reported
### Steps To Reproduce
1) `DbContext` with
```cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
throw new Exception("Bad DbContext");
}
```
2) Throw exception in any endpoint (doesn't matter if controller/razor page/minimal api)
The logs will report `Bad DbContext`; likewise the debugger will break there.
**Original exception will be buried very deep in debug output.**
### Exceptions (if any)
_See above_
### .NET Version
8.0.300
### Anything else?
```
.NET SDK:
Version: 8.0.300
Commit: 326f6e68b2
Workload version: 8.0.300-manifests.5273bb1c
MSBuild version: 17.10.4+10fbfbf2e
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.300\
.NET workloads installed:
[aspire]
Installation Source: VS 17.10.35004.147
Manifest Version: 8.0.0/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.0.0\WorkloadManifest.json
Install Type: FileBased
Host:
Version: 8.0.5
Architecture: x64
Commit: 087e15321b
.NET SDKs installed:
8.0.300 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 7.0.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 7.0.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 7.0.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.5 [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
global.json file:
Not found
```
This appears to be very similar to #32976, just under different circumstances
Contributor guide
Assessment
This issue has not been assessed yet.