EF 迁移捆绑包未打包迁移

问题描述 投票:0回答:1

我确信发布此内容后我会找出问题所在,但这里是。

我正在尝试在我的 DevOps 管道中获取 EF 迁移包,以便我可以使用 CD 管道将更新/更改部署到我的数据库。

下面是我的 yaml 文件。我省略了变量,因为有些变量很敏感

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: UseDotNet@2
      inputs:
        packageType: 'sdk'
        version: '8.x'
    - task: DotNetCoreCLI@2
      displayName: 'Install Dotnet EF Tool'
      inputs:
        command: custom
        custom: "tool"
        arguments: "install --global dotnet-ef"
    - task: DotNetCoreCLI@2
      displayName: EFBundle
      inputs:
        command: 'custom'
        custom: 'ef '
        arguments: 'migrations bundle --configuration Release --self-contained -r win-x64 -p "$(System.DefaultWorkingDirectory)\EntityFramework\EntityFramework.csproj" -o $(Build.ArtifactStagingDirectory)\SQL\bundle.exe --force'
    - task: PublishBuildArtifacts@1
      displayName: 'PublishArtifacts - SQL Bundle'
      inputs:
        pathToPublish: '$(Build.ArtifactStagingDirectory)/SQL'
        artifactName: 'SQL'
    - task: DotNetCoreCLI@2
      displayName: Restore
      inputs:
        command: 'restore'
        projects: |
          $(workingDirectory)\*.csproj
    - task: DotNetCoreCLI@2
      displayName: Build
      inputs:
        command: 'build'
        projects: |
          $(workingDirectory)\*.csproj
        arguments: '--output $(System.DefaultWorkingDirectory)\publish_output --configuration Release'
    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(System.DefaultWorkingDirectory)\publish_output'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)\$(Build.BuildId).zip
        replaceExistingArchive: true
    - publish: $(Build.ArtifactStagingDirectory)\$(Build.BuildId).zip
      artifact: drop

- stage: DeployToDevelopment
  displayName: Deploy to Development Slot
  dependsOn: Build
  condition: succeeded()
  jobs:
  - deployment: DeployDevelopment
    displayName: Deploy to Development Environment
    environment: 'development'
    pool:
      vmImage: $(vmImageName)
    strategy:
      runOnce:
        deploy:
          steps:
          - task: AzureCLI@2
            displayName: 'Azure SQL Schema Deployment'
            env:
              MonoConnectionString: $(devConnectionString)
            inputs:
              scriptType: 'pscore'
              scriptLocation: 'inlineScript'
              azureSubscription: '$(azureSubscription)'
              inlineScript: |
                cd $(Pipeline.Workspace)/SQL
                ./bundle.exe --connection "$(devConnectionString)" --verbose
          - task: AzureFunctionApp@1
            displayName: 'Deploy to Development App'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: 'functionApp'
              appName: $(devFunctionAppName)
              package: '$(Pipeline.Workspace)\drop\$(Build.BuildId).zip'

- stage: DeployToProduction
  displayName: Deploy to Production Slot
  dependsOn: DeployToDevelopment
  condition: succeeded()
  jobs:
  - deployment: DeployProduction
    displayName: Deploy to Production Slot
    environment: 'production'  # Make sure this environment has manual approvals configured in Azure DevOps
    pool:
      vmImage: $(vmImageName)
    strategy:
      runOnce:
        deploy:
          steps:
          - task: AzureCLI@2
            displayName: 'Run EF Migrations Bundle for Production'
            env:
              MonoConnectionString: $(connectionString)
            inputs:
              scriptType: 'pscore'
              scriptLocation: 'inlineScript'
              azureSubscription: '$(azureSubscription)'
              inlineScript: |
                cd $(Pipeline.Workspace)/SQL
                ./bundle.exe AddedEmailToInvitationTable --verbose
          - task: AzureFunctionApp@1
            displayName: 'Deploy to Production Slot'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: 'functionApp'
              appName: $(functionAppName)
              package: '$(Pipeline.Workspace)\drop\$(Build.BuildId).zip'

如您所见,我正在运行带有

dotnet ef migrations bundle
标志的典型
--force
命令,因此它会覆盖任何已存在的包(无论如何都不应该存在)。 我的 EF 迁移包含在一个名为
EntityFramework
的项目中。这是我的 IDE 中的屏幕截图。

Migrations in the left and db context setup on the right
正如您所看到的,我已经应用了迁移,并通过简单地调用

database update
(按预期工作)将它们部署到我的开发数据库。

我面临的问题是,一旦我的生产部署管道运行,我总是收到一条消息,说数据库已经是最新的。我一开始以为我的产品阶段可能正在查看我的开发数据库,但事实并非如此,因为您可以在此输出中清楚地看到它创建了一个 __EFMigrations 表


      Executed DbCommand (36ms) [Parameters=[], CommandType='Text', CommandTimeout='1800']
      SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (21ms) [Parameters=[], CommandType='Text', CommandTimeout='1800']
      SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (27ms) [Parameters=[], CommandType='Text', CommandTimeout='1800']
      CREATE TABLE [__EFMigrationsHistory] (
          [MigrationId] nvarchar(150) NOT NULL,
          [ProductVersion] nvarchar(32) NOT NULL,
          CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
      );
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (20ms) [Parameters=[], CommandType='Text', CommandTimeout='1800']
      SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (22ms) [Parameters=[], CommandType='Text', CommandTimeout='1800']
      SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (29ms) [Parameters=[], CommandType='Text', CommandTimeout='1800']
      SELECT [MigrationId], [ProductVersion]
      FROM [__EFMigrationsHistory]
      ORDER BY [MigrationId];
info: Microsoft.EntityFrameworkCore.Migrations[20405]
      No migrations were applied. The database is already up to date.

因此,为了解决该问题,我在

./bundle.exe
命令中添加了特定的迁移。如果您查看上面的 yaml,您会看到它名为
AddedEmailToInvitationTable
。当我运行该命令时,我收到一条错误消息,指出没有使用该名称的迁移;因此我得出的结论是,由于某种原因,我的
dotnet ef migrations bundle
命令有些看不到我的实际迁移`

System.InvalidOperationException: The migration 'AddedEmailToInvitationTable' was not found.

如何确保捆绑命令明确包含我的迁移文件夹?或者,是否有任何明显的我做错的事情?

entity-framework azure-devops entity-framework-migrations
1个回答
0
投票

是的,正如我在问题中提到的,一旦我发布,我就会找到答案。至少可以说这是一个奇怪的现象。

.MigrationsAssembly("EntityFramework")
添加到我的
AddDbContext
调用中似乎神奇地告诉 EF 迁移在哪里可以找到我的迁移,即使迁移程序集与我在呵呵上运行命令的程序集完全相同。

这是总代码

builder.Services.AddDbContext<MonoContext>(a =>
    a.UseSqlServer(dbConn, opts =>
            opts.CommandTimeout((int)TimeSpan.FromMinutes(30).TotalSeconds)
                .MigrationsAssembly("EntityFramework"))
        .EnableDetailedErrors()
        .EnableSensitiveDataLogging());

我通过尝试在我的项目上本地运行

dotnet ef migrations list
来解决这个问题。无论我做什么,它都不会列出任何迁移。所以我然后谷歌搜索“没有找到 ef core 的迁移”,并发现其他人在 ef 捆绑包的上下文之外也遇到了类似的问题。

© www.soinside.com 2019 - 2024. All rights reserved.