0

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?
17
  • Are you connecting to the right database? Make sure dc.Database.Connection.ConnectionString is what you're expecting. Commented Apr 22, 2013 at 20:25
  • yes, it's working with the right connection string :) Commented Apr 22, 2013 at 20:28
  • I have couple posts on that (and a migration initializer etc.) - but you need to post your initializer - that's the main thing. Otherwise that works w/o problems (other problems, not this one) Commented Apr 22, 2013 at 22:50
  • @NSGaga sorry, but i don't understand your comment. is not enough my test code?. This is the place where currently I am working, just a unit test (since i don't have any user interface application yet). may be you mean my dataContext and myconfiguration class? Commented Apr 22, 2013 at 23:31
  • sorry my bad :) I misread the 'initializer' line. Ok - if you already ran Add-Migration nothing will happen if you try to do it from the code - is that what you're doing? To actually test it - you'd need to clean the Db first, then run migration / initializer from code. Other than that, the test should run ok. Commented Apr 22, 2013 at 23:50

2 Answers 2

1

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.

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

Comments

0

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.

2 Comments

thank you for your feedback, the thing is that I am not working on asp.net mvc yet.. I just working on Domain/DAL project and a test project :)
@Rolando i added some more information, maybe that will help. But when you finally start working on your MVC project, don't forget to set the Initializer as suggested.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.