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?
Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.
Run Enable-Migrations command in a Package Manager console. This command added two more classes to your project in the Migrations folder. This migration was generated because Code First already created a database for us before we enabled migrations. It allows you to configure how Migrations behave for your context.
You can use DbContext with an edmx file, and you will still have an EntityConnection string rather than a Database ConnectionString when using this method.
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.
@Alexy Strakh recent comments spawned another argument worth putting in the answer.
Properly configuring a deployment system using Code First Migrations, given 2 ConnectionStrings.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With