azure GitHub操作数据库迁移成功运行,但表实际未创建

kpbwa7wx  于 2023-08-07  发布在  Git
关注(0)|答案(1)|浏览(84)

我有一个Blazor Webassembly应用程序,我正在将其部署到Azure应用服务。我使用的是Azure SQL Server数据库。我正在尝试使用Github操作设置CI/CD。

name: Build and deploy .NET Core application to Web App ForumApp
on:
  push:
    branches:
    - master
env:
  AZURE_WEBAPP_NAME: ForumAppServer
  AZURE_WEBAPP_PACKAGE_PATH: ForumApp\Server\published
  CONFIGURATION: Release
  DOTNET_CORE_VERSION: 7.0.x
  WORKING_DIRECTORY: ForumApp\Server
jobs:
  build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET SDK
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: ${{ env.DOTNET_CORE_VERSION }}
    - name: Restore
      run: dotnet restore "${{ env.WORKING_DIRECTORY }}"
    - name: Install .Net Tools
      run: dotnet tool install --global dotnet-ef    
    - name: Build
      run: dotnet build "${{ env.WORKING_DIRECTORY }}" --configuration ${{ env.CONFIGURATION }} --no-restore
    - name: Update Database
      run: dotnet-ef database update -s ForumApp\Server
    - name: Test
      run: dotnet test "${{ env.WORKING_DIRECTORY }}" --no-build
    - name: Publish
      run: dotnet publish "${{ env.WORKING_DIRECTORY }}" --configuration ${{ env.CONFIGURATION }} --no-build --output "${{ env.AZURE_WEBAPP_PACKAGE_PATH }}"
    - name: Publish Artifacts
      uses: actions/upload-artifact@v3
      with:
        name: webapp
        path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}  
  deploy:
    runs-on: windows-latest
    needs: build
    steps:
    - name: Download artifact from build job
      uses: actions/download-artifact@v3
      with:
        name: webapp
        path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
    - name: Deploy to Azure WebApp
      uses: azure/webapps-deploy@v2
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        publish-profile: ${{ secrets.ForumApp_D4E3 }}
        package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

字符串
应用程序已成功构建并发布。根据构建和部署的输出,数据库和表都已创建。但是,当我使用SQL Server Management Studio或Azure Data Studio查看数据库表时,这些表不可见。我不明白为什么。是否需要设置权限?
当我再次运行构建时,迁移也会再次运行,因此EF Tools似乎认为迁移也需要再次运行。也许我需要公布文物的迁移?会有什么问题呢?

Run dotnet-ef database update -s ForumApp\Server
Build started...
Build succeeded.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (515ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
      CREATE DATABASE [ForumDB];
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (159ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
      IF SERVERPROPERTY('EngineEdition') <> 5
      BEGIN
          ALTER DATABASE [ForumDB] SET READ_COMMITTED_SNAPSHOT ON;
      END;
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [__EFMigrationsHistory] (
          [MigrationId] nvarchar(150) NOT NULL,
          [ProductVersion] nvarchar(32) NOT NULL,
          CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
      );

...

more tables created ...

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
      VALUES (N'20230722161736_InitialMigration', N'7.0.9');
Done.


x1c 0d1x的数据

cczfrluj

cczfrluj1#

嗯,我想明白了,想把答案贴出来,以防有人来这里寻求帮助。
贷给https://dotnetthoughts.net/continuous-database-deployment-with-github-actions/
基本上,您将迁移生成为SQL文件,然后执行SQL语句来执行数据库更新。SQL语句可能包含CREATE、UPDATE、INSERT或DROP语句。SQL文件是通过执行
第一个月
下面是完整的yml文件:

name: Build and deploy .NET Core application to Web App ForumApp
on:
  push:
    branches:
    - master
env:
  AZURE_WEBAPP_NAME: ForumApp
  AZURE_WEBAPP_PACKAGE_PATH: ForumApp\Server\published
  CONFIGURATION: Release
  DOTNET_CORE_VERSION: 7.0.x
  WORKING_DIRECTORY: ForumApp\Server
jobs:
  build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET SDK
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: ${{ env.DOTNET_CORE_VERSION }}
    - name: Restore
      run: dotnet restore "${{ env.WORKING_DIRECTORY }}"
    - name: Install EF Tool
      run: |
          dotnet new tool-manifest
          dotnet tool install dotnet-ef
    - name: Build
      run: dotnet build "${{ env.WORKING_DIRECTORY }}" --configuration ${{ env.CONFIGURATION }} --no-restore
    - name: Generate scripts
      run: dotnet ef migrations script --project ForumApp\Server --output ${{ env.WORKING_DIRECTORY }}/sql/sql-script.sql --idempotent --context ApplicationDbContext --no-build
    - name: Azure SQL Deploy
      uses: Azure/sql-action@v1
      with:
        # Name of the Azure SQL Server name, like Fabrikam.database.windows.net.
        server-name: ${{ secrets.DB_SERVER }}
        # The connection string, including authentication information, for the Azure SQL Server database.
        connection-string: ${{ secrets.CONNECTION_STRING }}
        # Path to SQL script file to deploy
        sql-file: ${{ env.WORKING_DIRECTORY }}/sql/sql-script.sql
    - name: Test
      run: dotnet test "${{ env.WORKING_DIRECTORY }}" --no-build
    - name: Publish
      run: dotnet publish "${{ env.WORKING_DIRECTORY }}" --configuration ${{ env.CONFIGURATION }} --no-build --output "${{ env.AZURE_WEBAPP_PACKAGE_PATH }}"
    - name: Publish Artifacts
      uses: actions/upload-artifact@v3
      with:
        name: webapp
        path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}  
  deploy:
    runs-on: windows-latest
    needs: build
    steps:
    - name: Download artifact from build job
      uses: actions/download-artifact@v3
      with:
        name: webapp
        path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
    - name: Deploy to Azure WebApp
      uses: azure/webapps-deploy@v2
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        publish-profile: ${{ secrets.ForumApp_D4E3 }}
        package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

字符串

相关问题