4

As discussed in this thread, Entity Framework Code First is suited for a development environment. So, what steps or changes need to be made to an Entity Framework Code First application to move it over to Production?

  1. Migrate database to production server.
  2. Change the connection string in Web.config

Any changes needed to the DbContext initialization to keep it from monkeying around in a production environment? (Or will it stay out of trouble since the database schema is correct from the migrated database?) Anything else to do?

Community
  • 1
  • 1
Dan Sorensen
  • 11,403
  • 19
  • 67
  • 100
  • 1
    Check this question: http://stackoverflow.com/questions/5433318/how-do-i-allow-the-ef4-codefirst-database-initializer-to-run-under-development-b There are some very good ideas. – Ladislav Mrnka Apr 07 '11 at 18:19

2 Answers2

5

Here is a great article about the code first approach, including production deployment at the bottom: http://msdn.microsoft.com/en-us/magazine/hh126815.aspx

You could use a tool such as Red Gate's SQL Compare to analyze differences between your development environment and target environment to build change scripts that will allow your DBA to perform the migration.

Another thing you may want to do is add an entity called "AppVersion" to your code-first context. Then, override the Seed() method of your DropCreateDatabaseIfModelChanges IDatabaseInitializer so that it writes the current application version, or maybe the source control revision number using a continuous build.

Example:

public class OrderDbInitializer : DropCreateDatabaseIfModelChanges<OrderContext>
{
    protected override void Seed(OrderContext context)
    {

        context.AppVersion.Add(new AppVersion() {Version = Assembly.GetExecutingAssembly().GetName().Version.ToString()});
        context.SaveChanges();
    }


}

This way, you would always know which version of your code is associated to the database. You could also add more code to the Seed() method to create the sample data developers need to use while writing the application.

Finally, you would not want to use the DropCreateDatabaseIfModelChanges initializer in production, so using a config transform or some Factory pattern would need to be used.

Hope that helps give you some ideas.

densom
  • 695
  • 7
  • 8
1

There's no real tricks specific to ef-code first. It's the same things you would need to do to move any DB, using any ORM from test to prod.

Your steps above seem to cover 95% of what needs to be done (I'm leaving 5% open in case there's something I forgot :) )

taylonr
  • 10,732
  • 5
  • 37
  • 66