如何在使用Entity Framework 4.1创建的生产中的现有数据库上使用迁移?

问题描述:

我有一个在生产中的系统,该系统是使用Entity Framework 4.1 Code First创建的。现在,我已经升级到4.3,需要应用迁移,但是我需要介绍几个用例:

I have a system in production which was created with Entity Framework 4.1 Code First. Now, I have upgraded to 4.3 and need to apply migrations, but there's several use cases I need to cover:


  1. 新开发者需要从头开始使用种子数据创建的数据库。 ( Seed()方法还应用了一些唯一索引。)

  2. 生产环境仅需要应用未应用的更改。 (但是请记住,此数据库是在EF 4.1中创建的,没有迁移。)

  1. A new developer needs the database created from scratch with seed data. (The Seed() method also applies some unique indices.)
  2. The production environment needs only the unapplied changes applied. (But keep in mind that this DB was created in EF 4.1, which doesn't have migrations.)

如何创建迁移和一个初始化程序(或多个初始化程序)来涵盖这两种情况?

How do I create the migrations and an initializer (or initializers) to cover both these cases?

由于生产数据库是使用EF 4.1创建的,因此您需要做一些工作才能使其与迁移。从在开发环境中运行的当前生产代码的副本开始。 确保开发数据库不存在。

Since your production database was created with EF 4.1, you'll need to do a bit of work to get it ready for use with Migrations. Start with a copy of your current production code running in a dev environemnt. Make sure the dev database doesn't exist.


  1. 升级项目以使用EF 4.3(或更高版本)并创建初始迁移,以快照生产当前的状态。

  1. Upgrade the project to use EF 4.3 (or later) with Migrations and create the initial migration to snapshot what production currently looks like.

Update-Package EntityFramework
Enable-Migrations
Add-Migration InitialCreate  


  • 用匹配的迁移代码替换数据库初始化程序。

  • Replace your database initializers with matching Migrations code.

    对于种子数据,将其添加到 Migrations\Configuration.cs 文件的 Seed()方法中。请注意,与初始化器中的 Seed()方法不同,此方法在每次调用 Update-Database 时都会运行。它可能需要更新行(重置种子数据)而不是插入它们。 AddOrUpdate() 方法可以帮助解决此问题。

    For seed data, add it to the Seed() method of the Migrations\Configuration.cs file. Note that unlike the Seed() method in initializers, this method gets run every time Update-Database is called. It may need to update rows (reset the seed data) instead of inserting them. The AddOrUpdate() method can aid with this.

    由于现在可以使用Migrations创建唯一的索引,因此应将它们添加到 Up ()的I​​nitialCreate迁移方法。您可以使用将它们链接到 CreateTable()调用。 Index() 方法,或通过调用 CreateIndex() 方法。

    Since your unique indecies can now be created with Migrations, you should add them to the Up() method of the InitialCreate migration. You can either chain them off the CreateTable() calls using the Index() method, or by calling the CreateIndex() method.

    您可以使用 MigrateDatabaseToLatestVersion 初始化程序现在可以在初始化期间运行迁移。

    You can use the MigrateDatabaseToLatestVersion initializer now to run Migrations during initialization.

    获取脚本来引导您的生产环境。

    Get a script to bootstrap your production environment.

    Update-Database -Script
    

    从生成的脚本中,您将要删除几乎所有的表,因为表区域已存在。您需要的部分是 CREATE TABLE [__MigrationHistory] ​​ INSERT INTO [__MigrationHistory] ​​语句。

    From the script that gets generated, you'll want to delete almost everything since the tables aready exist. The parts you'll need are the CREATE TABLE [__MigrationHistory] and INSERT INTO [__MigrationHistory] statements.

    (可选)删除 EdmMetadata 表,因为不再需要它。

    Optionally, drop the EdmMetadata table since it is no longer needed.

    一旦您执行了这些操作,就应该很好地进行Migrations。新开发人员可以运行 Update-Database 从头开始创建数据库,并且您可以运行 Update-Database (或使用迁移初始值设定项),以在生产环境中应用其他迁移。

    Once you do these things, you should be good to go with Migrations. New developers can run Update-Database to create the database from scratch, and you can run Update-Database (or use the Migrations initializer) against production to apply additional migrations there.