发布后更新生产Sqlite数据库

时间:2016-10-31 20:07:45

标签: sqlite continuous-integration entity-framework-core .net-core

我们正在尝试在持续集成期间更新Sqlite数据库的模式。这是我们场景的简化版本。

部署脚本发布项目。

cd App
App> dotnet publish <args>

创建以下目录结构。

artifacts/
    App.dll
    Database.sqlite
    The-rest-of-the-publish-output
    web.config                
App/
    Migrations/
    Program.cs
    project.json
    Startup.cs

部署脚本随后会破坏迁移。

App> dotnet ef 
  --assembly ..\artifacts\App.dll 
  --startup-assembly ..\artifacts\App.dll 
  database update

问题是我们收到以下消息:

Unexpected value '..\artifacts\App.dll' for option 'assembly'

我们还尝试了其他方法在已编译的项目上运行dotnet ef database update,但无法确定如何更新artifacts目录中的数据库。

1 个答案:

答案 0 :(得分:2)

在我们目前的情况中,我们在Startup.cs上执行此操作:

if (migrateDb)
{
    try
    {
        using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>()
            .CreateScope())
        {
            serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
                    .Database.Migrate();
        }
    }
    catch { }
}

在开发migrateDb时解析为false,因此我们可以根据需要添加/删除和应用迁移。对于生产而言,为方便起见将解析为true

可能有更多有效/合适的选项,通常取决于上下文和/或项目需求。这只是许多人的一种方式。