在Docker容器中启动API时的待处理模型变更警告

后端开发 2026-07-11

我有一个基于ASP.NET Core 9的 Web API,数据库是SQL Server。

我在本地开发了一段时间,一切都没问题。

然而,当我尝试在Docker容器中启动它们时,每次在API启动时执行 context.Database.Migrate(),就会遇到让人头疼的 PendingModelChangesWarning

以下是我的 compose.yaml

services:
  myapi:
    build:
      context: .
      dockerfile: MyApi/Dockerfile
    ports:
      - "5000:5000"
      - "5001:5001"
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_HTTPS_PORTS=5001
      - ASPNETCORE_HTTP_PORTS=5000
    depends_on:
      sqlserver:
        condition: service_healthy

  sqlserver:
    image: mcr.microsoft.com/mssql/server:2025-latest
    environment:
      ACCEPT_EULA: "Y"
      MSSQL_SA_PASSWORD: "My_Pass"
    ports:
      - "1433:1433"
    volumes:
      - sqlserver_data:/var/opt/mssql
    healthcheck:
      test: [ "CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -U sa -P My_Pass -Q 'SELECT 1' -C || exit 1" ]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  sqlserver_data:

我的连接字符串定义在 appSettings.json

"ConnectionStrings": {
    "Default": "Server=sqlserver;User Id=sa;Password=My_Pass;Database=My_Database;Encrypt=False;"
},

在配置服务之前,我调用了以下种子方法:

public static void Seed(IServiceProvider serviceProvider)
{
    using (serviceProvider.CreateScope())
    {
        var context = serviceProvider.GetRequiredService<MyDbContext>();

        context.Database.Migrate();

        // Execute Custom SPs
    }        
}

我的API的 .csproj 文件中包含如下内容,因此应该包含 Migrations 文件夹:

<ItemGroup>
    <Folder Include="Migrations\" />
</ItemGroup>

这是我的Dockerfile:

# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 5000
EXPOSE 5001

# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["MyApi/MyApi.csproj", "MyApi/"]
COPY ["MyApi.Contracts/MyApi.Contracts.csproj", "MyApi.Contracts/"]
RUN dotnet restore "./MyApi/MyApi.csproj"
COPY . .
WORKDIR "/src/MyApi"
RUN dotnet build "./MyApi.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./MyApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApi.dll"]

没有报告任何连接错误(据我所知)。我原以为EF Core会在一个新的SQL Server上直接执行迁移,但是不是还需要做点额外的配置让它去执行?看起来现在它只是查看服务器,看到那里什么都没有,想让我手动运行迁移,但在处理Docker时,我不知道该怎么做。我一直在谷歌搜索,也和Docker Desktop的“Gordon”聊天,但遇到了瓶颈。

解决方案

经过进一步研究,这似乎是EF Core v9的一个已知问题。这里有一个较长的Github讨论串:https://github.com/dotnet/efcore/issues/34431。解决方法就是将你的上下文配置为忽略这些警告:

builder.Services.AddDbContext<MyDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("Default"))
     // following line is what you need:
    .ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)));

也有可能在EF v10中已经修复,但我还不想现在就往这个坑里跳。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章