0

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#.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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()?
You can't. Just create yet another migration (could be the first one) that inserts your startup data.
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.ColumnModel
Most probably your project doesn't target .net 4.5 but rather still targets 4.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.