6

I'm new to EF code first. I have an existing database in production and I used EF 4.3.1 code first and everything worked. Now I just updated my database schema and got the exception

System.InvalidOperationException: The model backing the 'MyDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269). 

I can't use DropCreateDatabaseIfModelChanges since it is in production, what's the simplest way to take to cope with the schema change?

Thank you.

4
  • 1
    Have you tried what the exception message suggested? Consider using Code First Migrations to update the database (go.microsoft.com/fwlink/?LinkId=238269). Commented Jun 11, 2012 at 14:25
  • I don't get it, you have (had) an existing database and you used code-first!? Let's assume you used code first and created the database with that approach then change your entity-model and update the database accordingly. The description of your scenario is a) wrong or b) somehow weird. Commented Jun 11, 2012 at 14:27
  • 1
    @YoupTube it's more common than you might think to "reverse engineer" and existing database with code-first. It's much more flexible than using the database first model designer. Commented Jun 11, 2012 at 14:30
  • @jrummel Ok. I don't have any experience with that approach but sounded strange to me. Commented Jun 12, 2012 at 10:13

6 Answers 6

6

Since EF-CF migrations are a fairly new concept I would suggest taking an age-old proven process and modifying it to work with our new tools, like EF. Here's what we do:

  1. Use DropCreateDatabaseIfModelChanges for local development. This will allow you to keep your local dev copy in sync with you Model (in code). Each time you build/run you get an updated local database. You can also use an Initializer to load test data, etc. Database.SetInitializer<DBContextNameHere>(new DBContextInitializerNameHere());

  2. Use RedGate's SQLCompare tool to compare local dev to production and automatically generate a change script for deployment. (Note: you can also auto-deploy from the tool)

http://www.red-gate.com/products/sql-development/sql-compare/index-b

The key benefit is you don't have to change your local dev process AND you get a repeatable and versioned deployment via the generated script. You can also combine this with their SQL Source Control tool to keep all of your SQL objects and deployment scripts (even data) in source control.

NO, I DO NOT work for these guys I just love their tool and how it helped me with this very same problem.

Sign up to request clarification or add additional context in comments.

Comments

4

You can look at using Entity Framework migrations to automatically update your database to match your new model.

http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx

1 Comment

This is what I did for this same issue and it worked perfectly.
3

I have found that running the following script

truncate table __MigrationHistory 

on the database removes this problem when your code / db becomes out of sync.

Maybe I'm just used to old school, build & tinker with database design, modify code accordingly, or discover I need a new field while coding etc... just add in both places.

I will admit to being new(ish) to MVC but sometimes these kind of features are unhelpful as they hide how things work on a fundamental level. When you understand how everything fits together, all good to have generators and tools to help, but when starting out... I don't think it's good.

I have come across many developers who do not understand simple concepts because of their use of code generators, wizards etc.. when it comes to programming for real, or maintaining legacy systems they are a bit lost..

1 Comment

I did a 'dirty' save which caused EF to add this table. Removing it fixed my issue. Thanks!
0

When this happens, I normally run the migration to point to a new database, once this database is created then compare the structure of the new one with your existing one, if need be do the changes manually (otherwise it may give you an indication of what is causing the error - unintended schema change or index etc) then remove the entries in your __MigrationHistory table on your original db and copy the entries across from your new db so the __MigrationHistory becomes correct and Code First will now be in sync correctly.

Comments

0

I resolved this issue by adding

Database.SetInitializer<MyContext>(null); to Application_Start() inside Global.asax.cs 

Comments

-1

I hope this code help full to you

EF- Code first migration

1 Comment

While the link may answer the question, it would be better if you can add a bit more detail in the answer here also. Doing so would make sure that your answer remains valid even if the link becomes inactive.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.