针对生产数据库的代码优先迁移 entity framework 使用 Web 配置转换更新数据库

Code first migration against production database entity framework update database with web config transformations

我有这个Web.config

  <connectionStrings>
    <add name="AuthContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=mokey;Integrated Security=True" providerName="System.Data.SqlClient" />
   </connectionStrings>

Web.Release.config

  <connectionStrings>
    <add xdt:Transform="SetAttributes" xdt:Locator="Match(name)" name="AuthContext" providerName="MySql.Data.MySqlClient" connectionString="server=mysql2.gear.host;database=mokey;persistsecurityinfo=True" />
  </connectionStrings>

所以我有两个数据库,本地数据库和生产数据库。

现在,当我 运行 update-database 本地数据库更新时。

如何运行 update-database 生产数据库,即将数据库迁移到最新版本?

您可能需要重新考虑使用代码部署更新您的 PRODUCTION 数据库。这不是 EF 的意图,许多 DBA 会遇到直接从代码更新的问题。

我们所做的是从 DBA 运行的迁移中生成一个脚本,如下所述:http://cpratt.co/migrating-production-database-with-entity-framework-code-first/#at_pco=smlwn-1.0&at_si=54ad5c7b61c48943&at_ab=per-12&at_pos=0&at_tot=1

您还必须考虑您的数据库初始化程序是什么,并在生产中将其设置为 null 或 migratetolatestversion。您还可以在构建服务器中使用 migrate.exe 来更新数据库。