I am using EF 6.1.2, to do Code based DB migration there are some commands we need to excecute. Which are: Add-Migration and Update-Database. I want this migration to happen in production environment. So is there any way to avoid Update-Database command in Package Manager Console and use C# APIs to do the same?
I can not use Update-Database command in production environment. And I want an aleternate to it in c#.
Add a comment
|
1 Answer
There is an obvious alternative, you can have the MigrateDatabaseToLatestVersion for this particular db context.
This way the migration happens automatically when a very first query is executed against the database. You could even have a separate commandline tool that does the same but is executed at the production environment on demand (rather than when the app executes the very first query), prior to application.
The initializer is straightforward to use, just call:
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<YourContext, Configuration>());
where Configuration is the class that keeps the configuration of your migrations.
4 Comments
Pavan
Thanks for the answer. But with this method I am into another issue. I am using
Database.SetInitializer(myDBInitializer), where myDBInitializer is MyDBInitializer: CreateDatabaseIfNotExists<DBContext>, its seed method is used to initialize my DB with some startup data (have to be inserted to DB only once). How to use 2 initializers with Database.SetInitializer()?Wiktor Zychla
You can't. Just create yet another migration (could be the first one) that inserts your startup data.
Pavan
Now I am using
Database.SetInitializer( new MigrateDatabaseToLatestVersion<YourContext, Configuration>()); for the coded migration. But I get a exception on AddColumn Method, Method not found: System.Data.Entity.Migrations.Model.ColumnModelWiktor Zychla
Most probably your project doesn't target .net 4.5 but rather still targets 4.0.