microsoft / microsoft/WindowsAppSDK
SQL Server LocalDB crash when trying to Add-Migration (WinUI 3)
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 4.7k
- Forks
- 471
- Avg merge
- 3d 13h
- Merged PRs (30d)
- 28
Description
### Describe the bug
ITNOA
I have a project based on WinUI 3, and I try to using Entity Framework Core with Generic Host in my Enterprise App.
Unfortunately, Entity Framework Core could not find any migration strategy when, I use `App.xaml.cs` (I think it is another bug), So I have to using `Program.cs` and `DISABLE_XAML_GENERATED_MAIN`.
After that, I can successfully create migration.
But for my CTO quality requirements, we have to use [DatabaseFactory](https://github.com/BSVN/Commons/blob/master/Source/BSN.Commons/Infrastructure/IDatabaseFactory.cs) and [UnitOfWork](https://github.com/BSVN/Commons/blob/master/Source/BSN.Commons/Infrastructure/IUnitOfWork.cs) patterns in Program.cs for Initializing Database like below
```csharp
using (var serviceScope = services.BuildServiceProvider().GetService().CreateScope())
{
BusinessDomainContext businessDomainContext = serviceScope.ServiceProvider.GetRequiredService().Get() as BusinessDomainContext;
businessDomainContext.Database.Migrate();
RecordsContext recordsContext = serviceScope.ServiceProvider.GetRequiredService().Get() as RecordsContext;
recordsContext.Database.Migrate();
}
```
In this case after I try to migration, My `LocalDB` crash.
Note: I try this approach in many many ASP.NET Core application, and we have not any problem.
### Steps to reproduce the bug
1. Type Add-Migration
2. Got crash
### Expected behavior
Create Migration successfully
### Screenshots

### NuGet package version
Windows App SDK 1.3.2: 1.3.230602002
### Packaging type
Packaged (MSIX), Unpackaged
### Windows version
Windows 11 version 22H2 (22621, 2022 Update)
### IDE
Visual Studio 2022
### Additional context
Full `InitializeDatabase` method like below
```csharp
private static void InitializeDatabase(IServiceCollection services)
{
string businessDomainConnection = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Kava.BusinessDomain;Integrated Security=True;MultipleActiveResultSets=true;";
string recordsConnection = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Kava.Records;Integrated Security=True;MultipleActiveResultSets=true;";
var businessDomainContextOptions = new DbContextOptionsBuilder();
var recordsContextContextOptions = new DbContextOptionsBuilder();
businessDomainContextOptions.UseSqlServer(businessDomainConnection);
recordsContextContextOptions.UseSqlServer(recordsConnection);
services.AddSingleton(businessDomainContextOptions.Options);
services.AddSingleton(recordsContextContextOptions.Options);
services.AddScoped();
services.AddScoped();
using (var serviceScope = services.BuildServiceProvider().GetService().CreateScope())
{
BusinessDomainContext businessDomainContext = serviceScope.ServiceProvider.GetRequiredService().Get() as BusinessDomainContext;
businessDomainContext.Database.Migrate();
RecordsContext recordsContext = serviceScope.ServiceProvider.GetRequiredService().Get() as RecordsContext;
recordsContext.Database.Migrate();
}
}
```
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the App.xaml.cs and Program.cs startup paths, then inspect InitializeDatabase and the DatabaseFactory/UnitOfWork usage shown in the report. Reproduce Add-Migration with the WinUI 3 project and the two LocalDB contexts, checking where LocalDB crashes during Database.Migrate(). Done means the failure is isolated and a verified fix or precise external dependency is identified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- database, desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100