【问题标题】:Using ASP.NET Core and EntityFramework 7 to update production DB with migration changes in Azure使用 ASP.NET Core 和 EntityFramework 7 在 Azure 中使用迁移更改更新生产数据库
【发布时间】:2016-04-06 14:15:21
【问题描述】:

我有一个在本地运行的完整网络应用程序,我现在正在努力将其投入生产。该应用程序目前位于 Azure 上,并正在查看 git 存储库以进行新部署,效果很好。

但是,该应用在其appsettings.json 中有一个连接字符串的连接,如下所示:

"database": {
  "connection": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Foo"
},

// In Startup()
var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddEnvironmentVariables();

// In ConfigureServices(...)
services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<FooDbContext>(options =>
        {
            options.UseSqlServer(Configuration["database:connection"]);
        });

这对于本地测试来说很好,但现在我已经准备好投入生产并且有一些问题(找不到好的文档)。

  1. 如何使用dnx 命令行将更改推送到生产数据库?一切都与应用程序定义的数据库静态绑定,因此默认情况下它总是会转到我的本地数据库。

  2. 在 Azure 中配置 DB 后,我是否只需要像这样在 Azure 的 Web 应用程序设置中修改我的连接字符串?

【问题讨论】:

  • 这篇文章解释了使用 dnx 进行 EF7 迁移。检查它msdn.microsoft.com/en-us/magazine/mt614250.aspx
  • 那篇文章没有解释如何将数据库更新应用到生产数据库。
  • 您(也)是否在本地处理迁移?
  • 是的,我有两个配置。一个用于本地,一个用于生产,每个都有一个连接字符串。
  • 你用的是RC1还是RC2?

标签: asp.net entity-framework azure asp.net-core asp.net-core-mvc


【解决方案1】:

基础知识

你说过你有两个配置。一个用于本地,一个用于生产,每个都有一个连接字符串。在这种情况下,如果您的生产配置名为 appsettings.production.json,您可以这样做:

启动()

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    .AddEnvironmentVariables();

配置服务(...)

services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<FooDbContext>(options =>
    {
        options.UseSqlServer(Configuration["database:connection"]);
    });

appsettings.production.json

{
  "database": {
    "connection": "Server=tcp:yr3d5dswl.database.windows.net,1433;Database=EFMigrationDemo;User ID=mvp2015@yr3d5dswl;Password=3f4g%^BD45bcE;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
  }
}

命令行

dotnet ef database update -e Production

这将更新您的生产数据库。

高级

与您的持续集成集成。通过在 project.json 中添加 postbuild 脚本,让迁移更新实时数据库。 Azure 将在每次部署时运行它。

"scripts": {
  "postbuild": [ "dotnet ef database update" ]
}

如果您想使用环境变量而不是 appsettings.production.json 文件,请查看 Setting the SQL connection string for ASP.NET 5 web app in Azure。这使您的用户名/密码不会出现在您的 git 存储库中。

参考文献

【讨论】:

  • 如何在本地测试生产 Azure SQL 连接字符串?我设置了set database:connection=my_production_connection_string 的环境变量,而我的Configuration.Get&lt;string&gt;("database:connection") 找不到任何环境变量。如果我检查环境变量,我会看到一个:Data:database:connection:ConnectionString。与我刚刚为database:connection 设置的值相同。为什么会变成这样?我怎样才能从我的代码中访问它,同时仍然允许我的 Development 环境也可以工作?
  • 你用什么方法检查环境变量?
  • @mariocatch 虽然我不确定为什么要转换它,但它似乎与约定有关。尝试使用Configuration.Get("Data:database:connection") 访问它。还将您的config.json 值设置为"Data" : { "database" : { "connection" : "..." } }。这里有更多细节stackoverflow.com/questions/31097933/…
  • 是的,关键看起来是使用格式"Data": { "my_db": { "ConnectionString": "..." } }。我现在正在 Azure 上测试它是如何工作的,如果有问题会回复。
  • 看起来dnx ef database update 的构建后步骤没有运行。我的数据库不是用那里的那一步创建的。此外,在检查 Kudu SCM 环境变量后,我在 Azure Web App 连接字符串 UI 中设置的连接字符串是:SQLAZURECONNSTR_Data:my_database:ConnectionString。那么,这可能就是为什么我所有的连接字符串查找在生产中都失败了?虽然我认为提供者应该修剪 azure 前缀并仍然找到/使用它。
猜你喜欢
  • 2022-01-17
  • 2019-11-21
  • 1970-01-01
  • 2015-06-27
  • 2020-09-18
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 2014-06-15
相关资源
最近更新 更多