sql-server - 如何将代码优先迁移应用到 Azure 中的数据库?

标签 sql-server entity-framework azure ef-code-first entity-framework-core

我正在开发 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/

相关文章:

azure - 如何将数组传递给 SQL 存储过程

.net - 在 Sql Server 中查找列的默认值的最佳方法

sql-server - SQL Server Native Client SQLNCLI11 和 SQLNCLI11.1 之间有什么区别?

c# - 使用 Entity Framework Code First 和 Fluent API 配置许多一对一关系

asp.net-mvc - Windows Azure + Asp.Net MVC + 电子商务

azure - StackExchange.Redis 超时

sql-server - 如何使用 Emacs 连接到 SQL Server?

c# - 尝试通过来自数据库触发器的 Web 请求执行 "EXECUTE Master.dbo.xp_CmdShell"命令以在本地创建文本文件时发生错误

c# - 不先查询就更新记录?

sql - 对象 'DF__*' 依赖于列 '*' - 将 int 更改为 double