What changes are needed to move an Entity Framework 4.1 Code First application to production?

∥☆過路亽.° 提交于 2019-12-21 05:39:10

问题


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?


回答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 :) )




回答2:


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.



来源:https://stackoverflow.com/questions/5585446/what-changes-are-needed-to-move-an-entity-framework-4-1-code-first-application-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!