使用连接字符串进行代码首次迁移 [英] Code First Migration with Connection Strings

查看:137
本文介绍了使用连接字符串进行代码首次迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我设法得到了Code First的运行,它的效果很好。



由于我仍在开发应用程序,数据库的结构尚未完成,因此我需要实现迁移。



我按照官方博客文章,并得到Update-Database命令的工作。



但是,这只会更新数据库的SQLExpress版本。数据库的生产版本在Azure上,我在运行时指定连接字符串,因此Update-Database命令不起作用。



所以我的最后一个问题是:如何将自动迁移应用于在运行时指定连接字符串的生产数据库?

解决方案

在包管理器控制台上,键入:



获取帮助更新数据库



相关部分:

 更新数据库[-SourceMigration< String>] [-TargetMigration< String>] [-Script] [-Force] [-ProjectName< String>] [-StartUpProjectName< String> ] [-ConfigurationTypeName< String>] [-ConnectionStringName< String>] [< Com 
monParameters>]

所以你可以做一个更新数据库-ConnectionStringNameMyConnectionString,它应该像一个魅力。



如果您设置了它,您还可以使用一个 MigrateDatabaseToLatestVersion (通过 Dabase.SetInitializer()),当您使用适当的连接字符串在生产部署应用程序时,在frist数据库访问上,应将数据库自动迁移到最新版本。



我建议谨慎,总是备份东西。



更新



@Alexy Strakh最近的评论产生了另一个值得投入的答案。 / p>

使用代码优先迁移正确配置部署系统,给定2 ConnectionString s。 / p>


  1. 使用默认密码定义web.config(prod和dev)中的连接字符串

  2. 有您的应用程序的配置系统知道产品和开发人员连接配置,可选择构建单元测试以确保正确选择*

  3. 使用配置文件转换,并将其转换为具有生产值

  4. 将您的软件包部署到生产(这应该是最前沿的方式

您不应与开发框中的生产环境进行互动,但如果您真的需要要做到这一点,那么这个临时解决方案一经完成就需要恢复。



另一个选择是简单地使用Web.Debug.config和Web.Release.config,并且有一个主要的web.config(这将是您在源代码控制中检入的唯一一个)的中心模板。



确保不会检查生产或个人开发密码(如果
任何)。



*您可以使用DEBUG符号来检查应用程序的运行方式。


So I've managed to get Code First running and it works great.

Since I am still developing the application the structure of the Database isn't finalize and so I need to implement migrations.

I followed the Official Blog Post and got that Update-Database command working.

However, this only updates the SQLExpress version of the database. The production version of the database is on Azure and I specify the connection string at run time so the Update-Database command doesn't work on that.

So my final question is: how do I apply automatic migrations to the production database whose connection string is specified at runtime?

解决方案

On the package manager console type:

Get-Help Update-Database

Relevant part:

    Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] [-ConnectionStringName <String>] [<Com
monParameters>]

So you can do a Update-Database -ConnectionStringName "MyConnectionString" and it should work like a charm.

You also have a MigrateDatabaseToLatestVersion database initializer, if you set it (via Dabase.SetInitializer()), when you deploy your app on production with proper connection string, on frist db access it should automagically migrate your db to the latest version.

I suggest caution though, always backup things.

Update

@Alexy Strakh recent comments spawned another argument worth putting in the answer.

Properly configuring a deployment system using Code First Migrations, given 2 ConnectionStrings.

  1. Define your connection strings in web.config (prod and dev), with default passwords
  2. Have the configuration system of your application know about the prod and dev connection configurations, optionally build unit tests to ensure the right one is picked*
  3. Employ config file transformation and have it transform your web.config into the one with production values
  4. Deploy your package to production (this should be the most cutting edge way)

You are not supposed to interact with the production environment from your development box, but if you really need to do that, then make it a temporary solution that needs to be reverted as soon as you're done.

Another option is to simply use the Web.Debug.config and Web.Release.config and have a central template for the main web.config (which would be the only one you check in in your source control).

Just make sure never to check in production or personal-development passwords (if any).

*You can use the DEBUG symbol to check how the application is running.

这篇关于使用连接字符串进行代码首次迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆