Amita Rai
Amita Rai

Reputation: 93

Entity Framework model change error

I am getting the error

"The model backing the 'DataContext' context has changed since the database was created. Consider using Code First Migrations to update the database".

I am using Entity Framework and have changed my model after declaring DataContext. How can I fix this error?

Upvotes: 9

Views: 11399

Answers (4)

abdelrhman raafat
abdelrhman raafat

Reputation: 161

Delete the __MigrationHistory table in SQL Server or just all rows of this table it should fix it

Upvotes: 0

mernig
mernig

Reputation: 371

I used database first to create a project after I had changed my database context and solved my problem:

Database.SetInitializer<Models.YourDbContext>(null);

Don't forget to handle the DbUpdateException

Upvotes: 1

giathienphu
giathienphu

Reputation: 137

If you delete the __MigrationHistory table in SQL Server it should fix it.

Upvotes: 11

Sławomir Rosiek
Sławomir Rosiek

Reputation: 4073

  1. If you already deployed your application or you don't want remove data from database you must read about Code First Migrations. Here you have a link: http://msdn.microsoft.com/en-us/library/hh770484(v=vs.103).aspx

  2. If you can delete database just do it. EF will create new database that match your model.

  3. You can also disable creating/updating database structure by invoking the following code:

    Database.SetInitializer<MyDbContext>(null);
    

Upvotes: 8

Related Questions