Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually editing database in code first entity framework

I have been trying out EF 4.1 (code first) with MVC 3. I am thinking ahead to when the application will need changes. I tested a couple of scenarios. I like the idea of manually editing the database when the model (my POCOs) would changed.

ASP.NET error when I change the model :

"The model backing the 'CTCMContext' context has changed since the database was created. Either manually delete/update the database..."

Now, it says that I can "manually update the database", but I did and still get the same error. Am I missing something !!?!

EDIT

Does this have to do with the model hash generate by EF ?

like image 886
billy Avatar asked May 18 '11 18:05

billy


1 Answers

I have had some struggles with this as well. I found that when you let EF create your database for you that a table named dbo.EdmMetadata is created and this is where/how EF tracks the state of the model. I found that if you delete this table after the database has been initially created you will put things into "manual mode" where you can now manually add/remove columns, tables, etc. from your database and EF will not throw the error that you are seeing.

If however you want to keep having EF update your database as you make changes to your model, you will need to create and call a ContextInitializer class that inherits from either DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways depending upon the behavior that you want to have happen.

like image 193
Paige Cook Avatar answered Oct 17 '22 04:10

Paige Cook