Entity framework code first - how to run Update-Database for production database

C#asp.net MvcEntity Framework

C# Problem Overview


I wanting to know how to run the 'Update-Database' command for a production database.

The 'Update-Database' database works fine from my local machine, but how do I get this to work for production data?

So, if I make a change to my application and then run the 'publish' through visual studio this works fine for the code side of things, but how do I run the 'Update-Database' command for the production data.

Hope this question makes sense...

Thanks,

C# Solutions


Solution 1 - C#

See https://stackoverflow.com/questions/10848746/using-entity-framework-code-first-migrations-in-production so that your application automatically updates the database when the Entity Framework initializes.

Now if you're more comfortable having manual control over the migration, you could use the -Script argument to the Update-Database command on your developer machine to generate SQL scripts which you can then run against the production database.

http://msdn.microsoft.com/en-us/data/jj591621.aspx (see Getting A SQL Script section)

Solution 2 - C#

To add on what @David said already...

Personally, I don't trust automatic updates in 'live' scenarios, and I always prefer manual database administration (i.e. there is a problem with permissions needed to create or alter Db - not to mention shared hosting etc.) - but from what I've seen migrations are pretty solid when it comes to synchronizing (in fact, the only way to 'untie' them normally is to remove the Db and force full/fresh update).

Here is a post I made a while ago on how to script and synchronize database / code and geared towards deployment scenarios (and when problems arise). It doesn't apply to you (yet) but something to keep in mind.

https://stackoverflow.com/questions/10254613/mvc3-and-code-first-migrations/10255051#10255051

Solution 3 - C#

Do you just want to automatically update the database to the latest version when and where ever your application runs (development and production)?

This may not be a good idea, except in very simple scenarios where you know you can trust automatic migration and manually migrating the database(s) is not feasible. Please see this answer: https://stackoverflow.com/a/15718190/2279059 . If you disregard this warning, read on.

Add the following to web.config:

<entityFramework>
<contexts>
  <context type="MyAssembly.MyContext, MyAssembly" disableDatabaseInitialization="false">
    <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MyAssembly.MyContext, MyAssembly], [MyAssembly.Migrations.Configuration, MyAssembly]], EntityFramework" />
  </context>
</contexts>

This may look scary, but it does basically the same as the following code:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());

If you have no luck with web.config, then you may also try to put this code into Global.asax. I personally prefer configuration over code.

If you want your config file to look cleaner, you can also derive a new class from the template MigrateDatabaseToLatestVersion class so you don't need to use the cryptic syntax for passing type arguments in your web.cofig file:

public class MyDatabaseInitializer : public MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration> {}

What this does is to set a database initializer which automatically updates the database to the latest version.

(Source and more details: Entity Framework Code First Web.config Initialization)

Solution 4 - C#

Another solution that has worked for me was this: In my local web.config file, I set the connection string to point to the production server.

Then all the PM Console commands will be run on the production server. I can update-database or revert migrations as needed, and the changes will apply to the production database.

Solution 5 - C#

You can run the Update-Database EF Tools command using the Production connection string as follows:

Update-Database -Args '--environment Production'

Source: https://docs.microsoft.com/en-us/ef/core/cli/powershell#aspnet-core-environment

As stated by others, you have to be extra careful, you should definitely try it first on a Staging environment.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhaydnDView Question on Stackoverflow
Solution 1 - C#David MooreView Answer on Stackoverflow
Solution 2 - C#NSGaga-mostly-inactiveView Answer on Stackoverflow
Solution 3 - C#Florian WinterView Answer on Stackoverflow
Solution 4 - C#GimmlyView Answer on Stackoverflow
Solution 5 - C#M. RuizView Answer on Stackoverflow