Skip to content
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

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

Open
soroshsabz opened this issue Jul 9, 2023 · 1 comment
Open

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

soroshsabz opened this issue Jul 9, 2023 · 1 comment

Comments

@soroshsabz
Copy link

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();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants