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 )
Florian Winter Apr 03 '17 at 12:44 on 2017-04-03 12:44
source share