.NET 10下dotnet ef升级流水线检测未决变更但本地正常的问题
问题背景
我们从.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'.
排查过程
- 最初怀疑是流水线
dotnet exec命令的问题,但本地复现同款命令后排除该可能。 - 使用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); - 目前临时解决方案是忽略该未决变更警告,但希望保留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

