Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't update Database with EF Code-First

I am using EF code-first migrations. I added a new property to my model but it doesn't reflect as a column in the database.

Configuration.cs

public Configuration()
{
    AutomaticMigrationsEnabled = true;
}

Global.asax.cs

Database.SetInitializer(new MigrateDatabaseToLatestVersion<DbContext, DbMigrationsConfiguration<DbContext>>());

using (var context = new MyDbContext())
{
    context.Database.Initialize(true);
}

I have already tried the update-database. It says no pending migrations.

EDIT:

My EF is a separate Class Library Project. There's another Class Library Project that stores all the Models. And the Main (Startup) Project is an MVC Project.

Where should I include Migrations?

Also, Add-Migrations Initial always gives me EMPTY Up() and Down() methods.

like image 403
who-aditya-nawandar Avatar asked Sep 10 '25 19:09

who-aditya-nawandar


1 Answers

this worked for me first of all take backup of your database then delete migrations folder in project and delete __MigrationHistory table in database then run this command

Enable-Migrations -EnableAutomaticMigrations -Force

then add migration

Add-Migration InitialMigration

and finally update database with

Update-Database
like image 151
Usman Avatar answered Sep 13 '25 22:09

Usman