microsoft / WindowsAppSDK

The Windows App SDK empowers all Windows desktop apps with modern Windows UI, APIs, and platform features, including back-compat support, shipped via NuGet.
https://docs.microsoft.com/windows/apps/windows-app-sdk/
MIT License
3.77k stars 317 forks source link

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

Open soroshsabz opened 1 year ago

soroshsabz commented 1 year ago

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 <DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>.

After that, I can successfully create migration.

But for my CTO quality requirements, we have to use DatabaseFactory and UnitOfWork patterns in Program.cs for Initializing Database like below

using (var serviceScope = services.BuildServiceProvider().GetService<IServiceScopeFactory>().CreateScope())
            {
                BusinessDomainContext businessDomainContext = serviceScope.ServiceProvider.GetRequiredService<IBusinessDomainDatabaseFactory>().Get() as BusinessDomainContext;
                businessDomainContext.Database.Migrate();

                RecordsContext recordsContext = serviceScope.ServiceProvider.GetRequiredService<IRecordDatabaseFactory>().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

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

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<BusinessDomainContext>();
    var recordsContextContextOptions = new DbContextOptionsBuilder<RecordsContext>();

    businessDomainContextOptions.UseSqlServer(businessDomainConnection);
    recordsContextContextOptions.UseSqlServer(recordsConnection);

    services.AddSingleton(businessDomainContextOptions.Options);
    services.AddSingleton(recordsContextContextOptions.Options);

    services.AddScoped<IBusinessDomainDatabaseFactory, BusinessDomainDatabaseFactory>();
    services.AddScoped<IRecordDatabaseFactory, RecordDatabaseFactory>();

    using (var serviceScope = services.BuildServiceProvider().GetService<IServiceScopeFactory>().CreateScope())
    {
        BusinessDomainContext businessDomainContext = serviceScope.ServiceProvider.GetRequiredService<IBusinessDomainDatabaseFactory>().Get() as BusinessDomainContext;
        businessDomainContext.Database.Migrate();

        RecordsContext recordsContext = serviceScope.ServiceProvider.GetRequiredService<IRecordDatabaseFactory>().Get() as RecordsContext;
        recordsContext.Database.Migrate();
    }
}
soroshsabz commented 1 year ago

related to https://github.com/microsoft/microsoft-ui-xaml/issues/8619