Perform Migration when Deploying to Azure, SQLite/.NET Core

.net-core azure entity-framework-core sqlite

Question

I'm trying to deploy my app to Azure, which is using SQLite with EF Core. It works properly when I run dotnet ef migrations add InitialCreate along with dotnet ef database update, but when I deploy it to Azure the table is not found. I tried using context.Database.Migrate(), it does create the file, though when I open it there's no schema, which is odd, and the same issue occurs. Any suggestions on how I can deploy to Azure and create migrations for my SQLite database would be much appreciated. Thank you.

1
3
3/10/2018 3:30:17 AM

Popular Answer

I followed a few suggestions on the web and finally found a solution that works for me. You can create a method called UpdateDatabase (or any other name you want) and call it in the Configure part of the Startup.cs.

private void UpdateDatabase(IApplicationBuilder app)
{
    using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
    {
        // get your database context of the current app
        using (var context = serviceScope.ServiceProvider.GetService<InsertYourDbContext>())
        {
            // Call the migrate of the database provider to
            // apply all data migrations pending
            context.Database.Migrate();
        }
    }
}
1
1/27/2020 3:22:38 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow