使用实体框架(code第一)的生产迁移实体、框架、code

2023-09-02 01:31:17 作者:硪清高゛自傲↗

我只是在寻找到使用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.

推荐答案

有一个数据库初始化程序,你可以用它来实现迁移到最新的版本在启动时(或更好的dbinitializer将踢在第一DB访问),该 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>());

对于具有每迁移一个文件,如果启用自动迁移,你会发现他们在迁移文件夹(默认),在项目的根目录。

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