They do not provide a way to do this before RTM, after which they promise a command line application and the msdeploy provider. Source: http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx
Of course, this is not satisfied, the powershell command is stored in the package directory and is plain text, it just loads the assembly called EntityFramework.Migrations.Commands stored in the same directory.
Tracing this assembly, I came to the following
public class MyContext : DbContext { static MyContext() { DbMigrationsConfiguration configuration = new DbMigrationsConfiguration() { MigrationsAssembly = typeof(MyContext).Assembly, ContextType = typeof(MyContext), AutomaticMigrationsEnabled = true, }; DbMigrator dbMigrator = new DbMigrator(configuration); dbMigrator.Update(null); } }
UPDATE: after a little experiment, I figured out a few more things
- Performing an update in the static constructor for your context is bad, since it interrupts the powershell commands, it is much better to add code to launch the application in another way (Global.asax, WebActivator or Main method).
- The above code only works when using AutomaticMigrations, you need to set MigrationsNamespace to load manually created migrations.
- The configuration class that I created should already exist in your project (added when installing the nuget migration package), so create an instance instead.
This means that the code is simplified to
DbMigrator dbMigrator = new DbMigrator(new NAMESPACE.TO.MIGRATIONS.Configuration()); dbMigrator.Update(null);
source share