Entity Structure First Code - How to Run Update-Database for a Production Database

I want 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 it to work on production data?

So, if I make changes to my application, and then run the “publish” through visual studio, this is great for the code part of things, but how can I run the “Update-Database” command for production data.

Hope this question makes sense ...

Thank,

+49
c # asp.net-mvc entity-framework
Mar 30 '13 at 5:23
source share
3 answers

Add what @David already said ...

Personally, I do not trust automatic updates to the "live" scripts, and I always prefer manual database administration (ie there is a permissions problem needed to create or modify Db - not to mention shared hosting, etc.), but from what I saw, migration is pretty solid when it comes to synchronization (in fact, the only way to “untie” them usually is to remove Db and force a full / new update).

Here is a post I made some time ago about how script and synchronize database / code focused on deployment scripts (and when problems arise). This does not apply to you (yet), but something needs to be kept in mind.

MVC3 and the first code migration - the model supporting the "blah" context has changed since the database was created

+16
Mar 30 '13 at 11:48
source share

See Using Entity Framework migration (code first) during production to have your application automatically update the database when initializing the Entity Framework.

Now, if it’s more convenient for you to have manual migration controls, you can use the - Script argument to the Update Database command on your development machine to create SQL scripts that can then be run from the production database.

http://msdn.microsoft.com/en-us/data/jj591621.aspx (see "Getting the SQL Partition"

+25
Mar 30 '13 at 8:40
source share

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

This may not be a good idea, except in very simple scenarios where you know that you can trust automatic migration and manually migrate database (s) is not possible. See this answer: https://stackoverflow.com/a/416829/ If you ignore 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 intimidating, but basically it looks like the following code:

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

If you're out of luck with web.config , you can also try putting this code in Global.asax . I personally prefer code tuning.

If you want your configuration file to look cleaner, you can also get a new class from the MigrateDatabaseToLatestVersion template MigrateDatabaseToLatestVersion , so you don't need to use critical syntax to pass type arguments in the web.cofig file:

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

This is done to install the database initializer, which automatically updates the database to the latest version.

(Source and more information: http://www.ralphlavelle.net/2012/09/entity-framework-code-first-webconfig.html )

+2
Apr 03 '17 at 12:44 on
source share



All Articles