我正在开发 ASP.NET Core API 应用程序,该应用程序托管在 Azure 中。开发和部署都很顺利,但是当我需要应用迁移时遇到了问题。
开发时我使用本地 SQL 数据库,部署时我依赖 Azure SQL 数据库。我在开发中通过用户 secret 提供连接字符串,在生产中我使用 Azure Key Vault 来读取连接字符串。当然,还有 ASPNETCORE_ENVIRONMENT
环境变量,我在开发时将其保留为 Development
,并在部署到 Azure 后更改为 Production
。但是,问题是,无论我的 ASPNETCORE_ENVIRONMENT
变量设置为 Development
还是 Production
,当我在开发和发布时创建新的迁移时来自 PowerShell 的 dotnet ef update database
,它只是更新我的本地数据库,而我的 Azure 数据库没有更改。有什么想法可以向 Azure 中的数据库进行迁移吗?
最佳答案
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
}
public void Configure(
IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
if (env.IsProduction())
using (var context = serviceProvider.GetService<MyContext>())
context.Database.Migrate();
}
关于sql-server - 如何将代码优先迁移应用到 Azure 中的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52758339/