Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest thing to do when database schema changed using EF code first

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.

like image 989
Ray Avatar asked Jun 11 '12 14:06

Ray


People also ask

How do you use code first when an existing database schema?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add.

What tool is specifically recommended to help manage database schema changes when updating your application code?

Use an online schema migration framework such as gh-ost or pt-online-schema-change. These tools create a "ghost" copy of each table you want to change, migrate the empty copy, and then incrementally copy data from the original table including any updates that happen during the migration.

How do I change the schema in Entity Framework?

There are 2 ways to change the schema, either by applying the TableAttribute or by implementing the interface IEntityTypeConfiguration<TEntity> . The first option won't help us because the schema is hard-coded. The second option gives us the ability to provide the schema from DbContext to the EF model configuration.

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.


1 Answers

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.

like image 163
kingdango Avatar answered Nov 15 '22 12:11

kingdango