部署到 Azure、SQLite/.NET Core 时执行迁移

Jac*_*ein 5 sqlite azure entity-framework-core .net-core

我正在尝试将我的应用程序部署到 Azure,它使用 SQLite 和 EF Core。当我运行 dotnet ef 迁移添加 InitialCreate 以及 dotnet ef 数据库更新时,它可以正常工作,但是当我将其部署到 Azure 时,找不到表。我尝试使用 context.Database.Migrate() ,它确实创建了文件,但当我打开它时没有架构,这很奇怪,并且出现了相同的问题。任何有关如何部署到 Azure 并为 SQLite 数据库创建迁移的建议将不胜感激。谢谢。

小智 4

我遵循了网上的一些建议,最终找到了适合我的解决方案。您可以创建一个名为 UpdateDatabase(或您想要的任何其他名称)的方法,并在 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)