在生产中使用实体框架(代码优先)迁移 [英] Using Entity Framework (code first) migrations in production

查看:34
本文介绍了在生产中使用实体框架(代码优先)迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是考虑在我们的项目中使用 EF 迁移,尤其是在版本之间的生产中执行架构更改.

I'm just looking into using EF migrations for our project, and in particular for performing schema changes in production between releases.

我已经看到有一个 API 可以使用 DbMigration 类在运行时执行这些迁移,但我找不到任何具体示例.

I have seen mentioned that there is an API to perform these migrations at run-time using the DbMigration class, but I can't find any specific examples.

理想情况下,我希望每个数据库更改都有一个 DbMigration 文件,并且这些更改在从当前版本到最新版本的应用程序启动时自动应用.

Ideally, I would want one DbMigration file for every database change, and for those changes to be applied automatically on application start up from the current version up to the latest version.

推荐答案

有一个 Database Initializer 可以用来在启动时迁移到最新版本(或者更好的是,dbinitializer 将在第一次访问数据库时启动),MigrateDatabaseToLatestVersion,你可以这样使用它:

There is a Database Initializer you can use to achieve the migration to latest version on startup (or better, the dbinitializer will kick in on first db access), the MigrateDatabaseToLatestVersion, you use it like that:

Database.SetInitializer<ObjectContext>(
    new MigrateDatabaseToLatestVersion<ObjectContext, Configuration>());

关于每次迁移有一个文件,如果您启用自动迁移,您将在项目根目录的 Migrations 文件夹(默认情况下)中找到它们.

Regarding having one file per migration, if you enable automatic migrations you will find them in the Migrations folder (by default) in the root of your project.

相关信息,包括示例,在这里:http://weblogs.asp.net/fredriknormen/archive/2012/02/15/using-entity-framework-4-3-database-migration-for-any-project.aspx

Relevant info, with examples, here: http://weblogs.asp.net/fredriknormen/archive/2012/02/15/using-entity-framework-4-3-database-migration-for-any-project.aspx

这篇关于在生产中使用实体框架(代码优先)迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆