Reputation: 12441
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.
Upvotes: 6
Views: 11240
Reputation: 1
I resolved this issue by adding
Database.SetInitializer<MyContext>(null); to Application_Start() inside Global.asax.cs
Upvotes: 0
Reputation: 117
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.
Upvotes: 0
Reputation: 61
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..
Upvotes: 3
Reputation: 3999
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:
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());
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.
Upvotes: 6
Reputation: 30628
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
Upvotes: 4