Tom
Tom

Reputation: 397

Code First Migrations not being executed

I got a problem. Somehow my CodeFirst Migrations are no longer executed. Everything worked perfectly before but not it does not work anymore. I deleted all the database now and tried to redeploy it, but the database is simply not updated anymore.

Any help?

( I got the checkbox in the publishing wizard checked to deploy CF Migrations)

Upvotes: 0

Views: 703

Answers (3)

jojoshua
jojoshua

Reputation: 143

I use this to force the migration to run.

var db = new MyDbContext();
db.Database.ExecuteSqlCommand("select 1");
db.Dispose();

Upvotes: 0

Tom
Tom

Reputation: 397

Ok I found the solution, so I will post it here if anyone else gets stuck here.

The Code First Migrations only get executed after a request is made. However, somehow my requests did fail because the database structure was not correct. So I could not do a request to execute the migrations, and therefor the database was not updated. So I created a simple dummy Request that just returns an OK status, and called it. This did trigger the migration and now everythign works. Weird.

Upvotes: 1

Yazan Ati
Yazan Ati

Reputation: 351

It is not working because you might have created/selected other connection in deploy wizard. Same is confirmed in the deployed connection string where you can see two connnection strings. The second connection string is also referecened in EF seciton - And, in the context you have used first connectionstring - public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) {} Changing name here will solve your issue.

Upvotes: 0

Related Questions