通过Azure DevOps中的EFCore迁移更新postgreSQL DB [英] Updating postgreSQL DB through EFCore migrations in Azure DevOps

查看:75
本文介绍了通过Azure DevOps中的EFCore迁移更新postgreSQL DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Azure DevOps中,我用于更新 SQL Server 数据库的方法是使用Entity Framework Core,它执行以下两项任务:

  1. 在我的构建管道中:

    这将是YAML(以防您要在发布管道之外使用它):

     -任务:DotNetCoreCLI @ 2displayName:'dotnet自定义'输入:命令:自定义自定义:工具参数:"install --global dotnet-ef --version 3.1.4 --ignore-failed-sources" 

    • 并且一旦安装了必需的工具以及 CMD Bash 任务,我们就必须执行以下脚本:

      dotnet ef数据库更新-c< DBCONTEXT>-p< PROJECT>-s< STARTUP_PROJECT>-v-不建立 

    如果您的项目中有多个上下文(有时其他DbContext可能来自某些块模块),则只需添加标志 -c .

    注意,我已经添加了标记-no-build ,因为我已经在构建管道中构建了该项目以遵循良好的实践.


    1. 另一个选择(也是我最后使用的那个),一直使用此任务基本上执行相同的过程,不同之处在于它使用已编译的 .dll 文件来完成此任务,因此您不必复制整个项目即可迁移工作.任务的设置,尽管您必须填写许多输入,但它非常简单,并且还应该与其他数据库一起使用.

    但是,如果我必须使用 SQL Server MySQL ,我将使用迁移脚本,因为此过程要容易得多(您只需要生成一个 .sql 脚本,这是部署迁移所需的唯一文件.

    In Azure DevOps, the way I used to update the SQL Server Database was with Entity Framework Core, using two tasks:

    1. In my Build Pipeline: This task that generated a sql script with my db migrations.
    2. In the Release Pipeline: This task to update the database using this script.

    The thing is, now that I'm using a PostgreSQL database, I can't find an easy and clean way to update the database in the same way. I've seen there's another task for MySQL that does exactly the same my release pipeline task did with SQL Server, but nothing for PostgreSQL.

    So I thought I could basically execute dotnet ef update database (with its proper options set) in the pipeline, but I was wondering if there's actually a way to keep updating the database in a smooth way as I did before.

    解决方案

    I finally got to fix it.

    There are two solutions I found to fix the issue.

    1. First, there's a common fix for all the databases that support Entity Framework Migrations:
      • Using a .NET Core Task, we'll have to install the dotnet ef tool: The task would look like this:

    And this would be the YAML (in case you want to use it in out of the release pipeline):

     - task: DotNetCoreCLI@2
      displayName: 'dotnet custom'
      inputs:
        command: custom
        custom: tool
        arguments: 'install --global dotnet-ef --version 3.1.4 --ignore-failed-sources'
    

    • And once we have the required tools installed, with a CMD or a Bash Task, we'll have to execute a script like this:

    dotnet ef database update -c <DBCONTEXT> -p <PROJECT> -s <STARTUP_PROJECT> -v --no-build
    

    You just have to add the flag -c in case you have more than one context in your project (sometimes the other DbContexts can come from some nugget packages).

    Notice I added the flag --no-build since I already built the project in the build pipeline to follow good practices.


    1. The other option (and the one I finally used), it's been to use this task that basically does the same process, with the difference that it does it by using your already compiled .dll files, so you won't have to copy the entire project to make the migrations work. The setup of the task, although you have to fill many inputs, it's pretty straightforward, and it's supposed to work with other Databases as well.

    However, if I had to use SQL Server or MySQL I would use a migrations script, since the process it's much easier (you just need to generate a .sql script and then it's the only file required for deploying the migrations).

    这篇关于通过Azure DevOps中的EFCore迁移更新postgreSQL DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆