You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 10下dotnet ef升级流水线检测未决变更但本地正常的问题

.NET 8迁移至.NET 10后EF Core未决变更流水线报错排查

问题背景

我们从.NET 8迁移到.NET 10后,多个项目因未决变更导致迁移失败,多数通过新增迁移解决,但有一个无任何代码变更的项目出现异常:

  • 本地执行dotnet ef database update完全正常,甚至在本地用流水线同款dotnet exec命令执行也无问题,连指向Azure DB运行都正常:
    dotnet exec  --depsfile WebApi.deps.json  --runtimeconfig WebApi.runtimeconfig.json tools\.store\dotnet-ef\10.0.1\dotnet-ef\10.0.1\tools\net10.0\any\tools\net10.0\any\ef.dll database update  --assembly Infrastructure.dll  --startup-assembly Infrastructure.dll  --verbose  --root-namespace Infrastructure  --context myDbContext  --connection "Server=localhost;Database=myDb;Trusted_Connection=true;Encrypt=false;"
    
  • 但在Linux流水线中执行时,直接报错:

    System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'myDbContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.

排查过程

  1. 最初怀疑是流水线dotnet exec命令的问题,但本地复现同款命令后排除该可能。
  2. 使用EF Core模型差异对比工具代码检测变更,本地运行未发现任何差异,新增空迁移也无法解决问题:
    // Initialize the snapshot model with runtime dependencies before getting its relational model
    // The snapshot model is already finalized, but needs runtime initialization
    var initializedSnapshotModel = modelRuntimeInitializer.Initialize(snapshotModel, validationLogger: null);
    var snapshotRelationalModel = initializedSnapshotModel.GetRelationalModel();
    
    // Get the current model from the design-time model if available (for full configuration),
    // otherwise use runtime model. Note: IDesignTimeModel is only available at design-time.
    // At runtime, we use context.Model which may not have all configuration details.
    IModel currentModel;
    
    try
    {
        // Try to get design-time model (only available at design-time)
        var designTimeModelService = context.GetService<IDesignTimeModel>();
        currentModel = designTimeModelService?.Model ?? context.Model;
    }
    catch
    {
        // Fallback to runtime model if design-time model is not available
        currentModel = context.Model;
    }
    
    var currentRelationalModel = currentModel.GetRelationalModel();
    
    var differences = migrationsModelDiffer.GetDifferences(snapshotRelationalModel, currentRelationalModel);
    
  3. 目前临时解决方案是忽略该未决变更警告,但希望保留EF的模型变更保护机制。

关键发现(更新)

将对比代码部署到Azure容器(Linux环境)运行后,检测到明确差异:

Found 1 pending model change(s): - Microsoft.EntityFrameworkCore.Migrations.Operations.UpdateDataOperation

推测差异来源于Windows/Linux环境差异或Debug/Release编译配置差异,而非流水线本身的问题,正在重新部署以获取更详细的变更日志。

疑问

  • EF Core在Windows和Linux下的模型对比逻辑是否存在差异?
  • 如何进一步排查流水线环境中触发未决变更的具体细节?

内容的提问来源于stack exchange,提问作者The Lemon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 18:05:12