3

While code-first is great for deployment and while in development I fail to see how you can push through changes made to your domain-model in a code-first way after going in production.

What do I do with the data accumulated while we were in production?

Am I supposed to manually migrate the data from the Version A schema to Version B schema. Do I need to code around the schema to prevent breaking changes? Do I say goodbye to code-first after the initial deployment and switch to database-first?

What am I missing?

ndsc
  • 1,173
  • 2
  • 13
  • 22
  • 1
    I think you need Code First Migrations, it is included in version 4.3, see: http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx – Marthijn Feb 24 '12 at 13:25

3 Answers3

2

First a disclaimer, I don't have much experience with EF and I would assume it is similar in this regard to nHibernate. I answered similar question here. The bottom line is that EF and NHibernate are just an ORM frameworks. They have intimate knowledge of your domain but only in its current state, they don't know history. ORM can generate database schema, but this feature is only usefully for initial rollouts and integration testing. You can not rely on it in a production application that evolves and need upgrades (to both schema and data).

In my experience there is no magic tool that will write upgrade scripts, they have to be written manually or at least reviewed by developer. Tools can provide you a framework for executing these scripts, like RoundhouseE. Scott Allen has an excellent series about 'forward-only, run-once' approach.

Community
  • 1
  • 1
Dmitry
  • 17,078
  • 2
  • 44
  • 70
2

As mentioned in the comment by @Henkie, EF Data Migrations attempt to solve the exact problem you are describing.

I have an application using code-first EF in production now, and what we did was build a little schema update strategy along side EF. We have a table that understands what version the database is currently on and a directory of sql scripts that run (migrating data, changing schema, and incrementing the version table).

links:

  1. Code-Based

  2. Automatic

Hope this helps.

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
2

With EF 4.3. you need to use Migrations, and you can do it with an existing database.

This is a great blog post about exactly what you're talking about: Using EF 4.3 Code First Migrations with an Existing Database from Julie Lerman.

I'm also blogging about this right now: Using Entity Framework Code First with an existing database.

Ralph Lavelle
  • 5,718
  • 4
  • 36
  • 46