microsoft / microsoft/WindowsAppSDK

SQL Server LocalDB crash when trying to Add-Migration (WinUI 3)

Open
#3,715 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

area-External
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

![image](https://github.com/microsoft/WindowsAppSDK/assets/17947618/0108adbe-a975-4f15-ae94-6ccc2942ed56)

### 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.