我的工作方式是在我的开发环境中配置上下文
网络配置
<add name="MyContext" connectionString="Data Source=.\SQLDev2008R2;Initial Catalog=My_DEV;Persist Security Info=True;Integrated Security=SSPI;Connection Timeout=240" providerName="System.Data.SqlClient" />
然后,我在发布生产版本时使用SlowCheetah转换该连接字符串
web.Release.config
<connectionStrings>
<add name="MyContext"
connectionString="Data Source=(local);Initial Catalog=My_PROD;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
当应用程序在生产环境中运行时,我允许自动应用迁移。Enable-Migrations -EnableAutomaticMigrations
您可以使用(加上其他适当的参数)配置此选项。
这可能不适合某些环境。如果是这种情况,在您update-database
在开发环境中运行之前,请先运行update-database -Script
(使用适当的参数)。这将生成一个 SQL 脚本,当您准备好应用给定的更新时,您可以在生产数据库中运行该脚本,而不是依赖于自动迁移。