Jacob Stein
Jacob Stein

Reputation: 322

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

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.

Upvotes: 5

Views: 497

Answers (1)

Sandro Klostermann
Sandro Klostermann

Reputation: 91

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();
        }
    }
}

Upvotes: 4

Related Questions