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

Azure发布Web API时EF Core迁移未应用及连接字符串问题求助

Solutions to Your EF Core Azure Deployment Issues

Issue 1: Connection Strings in Environment-Specific Appsettings Not Recognized

Possible Causes

  • Environment-specific appsettings.*.json files aren't being copied to the publish output.
  • The ASPNETCORE_ENVIRONMENT variable isn't set correctly in Azure, so the app isn't loading the right config file.
  • Azure's publish process prioritizes the root appsettings.json over environment files if there's a configuration loading mismatch.

Fixes

  • Ensure environment files are included in publish:
    Right-click appsettings.Development.json and appsettings.Production.json in Solution Explorer → Properties → Set Copy to Output Directory to Copy if newer.
  • Set the environment variable in Azure:
    Navigate to your App Service → Configuration → Application Settings → Add a new entry with key ASPNETCORE_ENVIRONMENT and value Production (or your target environment).
  • Use Azure's native connection string store (Recommended):
    Go to App Service → Configuration → Connection Strings → Add your production connection string here. This value will automatically override any appsettings entries, keeping sensitive data out of your codebase.

Issue 2: Migrations from Domains Project Not Applied During Publish

Possible Causes

  • The BookingAPI project doesn't explicitly reference the assembly containing your migrations (the Domains project).
  • EF Core can't locate the migration files because the DbContext is in a separate class library.

Fixes

  • Specify the migration assembly in Program.cs:
    When registering your ApplicationDBContext, explicitly tell EF Core where to find migrations using the Domains assembly:
    builder.Services.AddDbContext<ApplicationDBContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("YourConnStringKey"))
               .MigrationsAssembly(typeof(ApplicationDBContext).Assembly.FullName));
    
  • Validate project references:
    Confirm BookingAPI has a project reference to the Domains class library (Right-click BookingAPI → Add → Project Reference → Select Domains).
  • Configure publish settings to include migrations:
    In Visual Studio's publish profile wizard, go to the Database tab → Expand Entity Framework Core Migrations → Select your ApplicationDBContext (it should appear after setting the migration assembly). Enable the option to apply migrations during publish.

Bonus: Local Migration Command

When creating migrations locally from the Domains project, use this command to target the BookingAPI as the startup project:

dotnet ef migrations add YourMigrationName --startup-project ../BookingAPI --project .

内容的提问来源于stack exchange,提问作者Srijani Ghosh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 12:46:24