4

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.

4 Answers 4

2

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 
Sign up to request clarification or add additional context in comments.

7 Comments

Almost there. Now at least I have code in the Up() and Down() Methods. But I am stuck at "There is already an object named <tablename> in the database."
try Add-Migration InitialMigration -IgnoreChanges
and also set AutomaticMigrationsEnabled = false;
I already tried Add-Migration InitialMigration -IgnoreChanges. Then I lose all the code in the Up() and Down() methods and the DB doesn't get updated with the new column. Will try with AutomaticMigrationsEnabled = false;
It doesn't work. I am stuck with "There is already an object named ....... in the database." "Add-Migration InitialMigration -IgnoreChanges" rescaffolds but nothing happens.
|
0

Verbose can fix this problem. In your package manager console run

Update-Database –Verbose 

Comments

0

Before executing "Update-Database" command did you run "Add-Migrations"? The Add-Migrations command will generate the necessary migration script to update the database.

Also, do you have a _MigrationsHistory table in your database?

Could you update your code to point to your MyDbContext rather than the base DbContext? And I believe DbMigrationsConfiguration<DbContext>>() should also point to your dbcontext type.

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, DbMigrationsConfiguration<MyDbContext>>()) 

3 Comments

I had the _MigrationsHistory. I deleted it and ran update-database again. I also tried with the Add-Migrations command, didn't work. Also, since I already have AutomaticMigrationsEnabled, I shouldn't be forced to use Add-Migrations....right?
Ah yes, that is correct. You shouldn't need to explicitly run an add-migrations command. The Auto migration should pick it up with the update-database. I am confused by one statement in your code - is your context DbContext or MyDbContext?
Its MyDbContext.
0

*Add Your New Model Frst: User Model -id,fname,lname,adress,ect...

*Define in your Application db context the new model as a new set Example : public DbSet Users{ get; set; }

*Create Migration : add-Migration YourMigrationName *Update Database

2 Comments

@Billa how and what do u mean exactly ? adding styles to my comment ?
don't write comments in answer section.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.