45

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?

Sylvain Rodrigue
  • 4,751
  • 5
  • 53
  • 67
Dragonseer
  • 2,874
  • 7
  • 29
  • 42
  • For those checking with NO app.config and not using PM console , see this post http://stackoverflow.com/questions/15504465/entityframework-code-first-custom-connection-string-and-migrations/16133150#16133150 – phil soady Apr 22 '13 at 06:52

2 Answers2

62

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 Database.SetInitializer()), when you deploy your app on production with proper connection string, on first 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.

Jim Wolff
  • 5,052
  • 5
  • 34
  • 44
WDRust
  • 3,663
  • 1
  • 19
  • 23
  • I have a dev database specified in my default web.config and prod db connection specified at web.release.config. I want to use prod instead of dev, can I do that? – Mando May 08 '16 at 08:33
  • Yes, by all means, but remember that it is NOT entity framework (or any storage technology) responsibility to be aware of your configuration logic for dev/staging/production. I think what you need is config file transformation. https://msdn.microsoft.com/en-us/library/dd465318%28v=vs.100%29.aspx – WDRust May 08 '16 at 21:18
  • That is, assuming you wanna go down that route. If you have additional details or requirements you should make another question (feel free to ping me) – WDRust May 08 '16 at 21:20
  • I guess my question was not really specific, let me try to provide some details. I have asp.net project with EF6.1 code first and data migration. I have two configuration: `dev`, `prod`. The `dev` configuration is define in web.config. I have defined transformation file for my `prod` configuration as well, so every time I'm switching to prod configuration transformation is applied. The issue is that nuget manager when I call update-database is continue to use my `dev` config, even if I switched to prod. So I have to open web.config (default) and temporary define there a prod database connectio – Mando May 08 '16 at 21:29
6

Why does Entity Framework's EF Migrations Add-Migration step require a database connection string?

Has a solution which i consider less labor intensive in the long run. As if you create a connection string with the same name

On your context :base("DBName")

Connection string name and initial catalog match the DBName you specified and you don't need to enter the connection string name every time.

Community
  • 1
  • 1
P6345uk
  • 703
  • 10
  • 26
  • Agree this technique works fine, but there is a catch. If you have separated your data access library into a separate project and not put the proper db configuration into your "default" project Add-Migration will apply a default connection string of "./SQLEXPRESS". I was able to move around this problem by putting the connection string information into my default project app.config file. This is odd behavior because I had changed the default project in Package Manager Console to my Data Access project. But, Visual Studio 2017 seemed to ignore this fact when looking for the DB Connection – Jeff Sep 19 '18 at 18:31
  • Do you have an example of this happening if so I will take a look. It maybe that visual studio 2017 is slightly different . From memory the config file it uses is from the startup project and it ignores the config file in the sub project – P6345uk Sep 19 '18 at 18:41