Rolando
Rolando

Reputation: 762

EF Code First Migrations: SetInitializer and Initialize

I am working on Code First Migration and Im trying work with code based migrations (Automatedmigration=false). What I can to do now it's:

-Run Enable Migrations

-Add migration Initial

-Run migrations using "Update Database" (To create the migrationhistory table)

-Make changes in my model

-Execute Add Migration "changesInMyModel"

Now what i am trying to do is be able to run all my migrations automatically. using something like:

[Test] 
public void UpdateDataModel()
{
 Database.SetInitializer(
 new MigrateDatabaseToLatestVersion<MyContext, MyConfiguration>());
 var dc = new MyContext();
 dc.Database.Initialize(true);
}

After to execute the test and go to the database i can't see any of my changes. What Can i do? advices are welcome!

UPDATE: Myconfiguration class is using Automatedmigration=false.

MyConfiguration class is public and visible outside of its parent project.

I am not using any configuration in the app.config since that I am using Database.SetInitializer from the source code.

Before to run my test MigrationHistory is created

Running update database from package manager console the migrations run fine.

UPDATE: I am running a version modified (using code based migration) of the example bellow: http://msdn.microsoft.com/en-us/data/jj591621.aspx , but the probleme is still there. - Enable migration - Add migration - Try SetInitializer and Initialize but nothing happen.

UPDATE: - SetInitializer and Initialize working fine when Automatedmigration=true, the changes in the model are sync with the db. - Using the code bellow works.

new DbMigrator(new Configuration()).Update(); What should be the difference?

Upvotes: 0

Views: 4934

Answers (2)

NSGaga
NSGaga

Reputation: 14302

And just to update based on our comments in the thread...

This solved the problem - but to be frank I'm still not sure why it did :)

Try with EnableAutomaticMigrations on/off just to check. Try putting try-catch to make sure no errors. And try running DbMigrator directly new DbMigrator(new Configuration()).Update()

...and additional comments

Migration should work w or w/o that flag. And DbMigrator does exactly what that initializer does. There seems to be no difference - this is the source code (not same EF ver - but I think it's the same) - MigrateDatabaseToLatestVersion.

Upvotes: 1

Komengem
Komengem

Reputation: 3764

My guess is you should try to place the code below under protected void Application_Start() in the Global.asax.cs. This way, you database connection will always be Initialized before everything else.

if (!WebSecurity.Initialized)
            WebSecurity.InitializeDatabaseConnection("DefaultConnection", 
           "UserProfile", "UserId", "UserName", autoCreateTables: true);

Maybe this Pro Asp.Net Mvc 4 will help you set up your testing project. There is also an MVC 3 version, but they will pretty much say the same thing. They suggest to use Moq Library, and its helped me in the past.

Upvotes: 0

Related Questions