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.*.jsonfiles aren't being copied to the publish output. - The
ASPNETCORE_ENVIRONMENTvariable isn't set correctly in Azure, so the app isn't loading the right config file. - Azure's publish process prioritizes the root
appsettings.jsonover environment files if there's a configuration loading mismatch.
Fixes
- Ensure environment files are included in publish:
Right-clickappsettings.Development.jsonandappsettings.Production.jsonin Solution Explorer → Properties → Set Copy to Output Directory toCopy if newer. - Set the environment variable in Azure:
Navigate to your App Service → Configuration → Application Settings → Add a new entry with keyASPNETCORE_ENVIRONMENTand valueProduction(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 yourApplicationDBContext, 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 yourApplicationDBContext(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
相关产品推荐
相关产品推荐

