无法与现有的数据库上运行EF5迁移 [英] Unable to run EF5 migration with existing database

查看:282
本文介绍了无法与现有的数据库上运行EF5迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我读过这些问题/答案:

  • EF-迁移消息
  • <一个href="http://stackoverflow.com/questions/11204900/how-can-i-stop-add-migration-checking-my-database-has-no-pending-migrations-when">How我可以停止添加,迁移检查使用$ C $基于C语言的迁移,当我的数据库没有未处理的迁移?
  • <一个href="http://stackoverflow.com/questions/9924946/make-ef4-3-$c$c-first-migrations-ignore-pending-migrations">Make EF4.3 $ C C首先迁移$忽略挂起的迁移

这些都似乎是EF之前的版本EF5,和我的情况似乎并不适合这些答案。所以,让我描述我的处境。

  1. 在我的应用程序最初是先用EF4,模型创建。我设计的GUI设计数据库,并用它来生成我的数据库。
  2. 我一直在运行并收集数据到数据库几个月。我真的不能失去这个数据。
  3. 在我支我的code,安装EF5用的NuGet,以及用于EF电动工具所生成从我的数据库我的模型右键单击一个新的类库项目,并选择实体框架|反向工程code首先
  4. 在我能够顺利地重新提到我的新项目,将我的项目,以使用新的DbContext代替的ObjectContext,并取消了EF4类库抓住我的旧模式。该方案的伟大工程!

现在,我想尝试的自动迁移,我已经on Rails的有经验的点点与红宝石。这是我做的:

  1. 启用-迁移。有一点麻烦,由于连接字符串,并逐渐习惯它的app.config,但最终得到了它。但是,这个MSDN页面说,这应该有自动生成的第一次迁移,让我给点我敢已经在。事实并非如此。
  2. 添加迁移InitialSchema 来完成的任务并没有在步骤1中自动完成这个工作。
  3. 添加属性,以我的模型对象之一,然后试图运行添加迁移AddSerialToLogEntries ,并presented有:
  

无法生成一个明确的迁移,因为下面的   明确的迁移正在等待:[201307190100268_InitialSchema]。   试图产生一个之前应用未决明确的迁移   新的明确的迁移。

试图以应用迁移对现有数据库失败,这并不奇怪。

我上面提到的基本上是其他的答案说,我的运气,但就像我说的是旧版本的实体框架。难道我有什么选择这里?

在写这个问题,我想我或许可以使用SQL Server Management Studio来我的数据导出到一个SQL脚本,删除整个数据库,让EF创建它,然后运行该脚本来获取我的数据。最..我会尽力,明天当我有时间,但我想听到的话,还有其他的选择,因为我不是100%肯定会工作,真舍不得让在过程中插入的数据的任何错误。

解决方案
  

冉启用-迁移。有一点麻烦,由于连接字符串   并逐渐习惯它的app.config,但最终得到了它。然而,   这个MSDN网页说,这应该已经自动生成了   首先迁移到让我给点意见,我已经在。事实并非如此。

     

冉加入迁移InitialSchema完成什么不   在步骤1中这个工作自动完成的。

事实上,启用-迁移命令创建只有当你的数据库已经创建了code-首先在初始迁移前,在这种情况下,该数据库包含一个__ MigrationHistory 表<$ C C $>。如果这个表不存在(这是这种情况,当你有从未之前已经创建了code-一是现有的数据库)启用-迁移只创建配置类。你要叫添加迁移手动然后创建第一个迁移类。所以,你看到的是正常现象。

通常的程序prepare用于迁移现有的数据库,如果您使用的是EF 5如下:

  • 呼叫启用-迁移的包管理器控制台。 A 迁移文件夹中的项目和配置类将被创建。

  • 在构造函数中打开配置类,并设置 AutomaticMigrationsEnabled = FALSE (如果不是已经默认情况下)。

  • 在包管理器控制台呼叫

     添加迁移-IgnoreChanges InitialSchema
     

    InitialSchema仅仅是一个例子名称。像你想,你可以将其命名为。 A &LT;时间戳&GT; _InitialSchema 类将被创建,源于 DbMigration 。该向上方法,这个类是空的,由于 -IgnoreChanges 标志。如果没有这个标志的类将包含一个迁移到你的整个模型添加到数据库,是不是你想要的,因为现有的数据库已经包含了数据库模式是什么。

  • 运行的包管理器控制台更新数据库。因为向上方法是空的。此更新没有与你现有的模式的除了的它创建了 __ MigrationHistory 表(如数据库系统表),并增加了第一条记录到此表包含当前EF模型的模型哈希值。

  • 可选的最后一步:如果preFER与自动迁移打开配置类,并设置 AutomaticMigrationsEnabled =真正的工作在构造函数中。如果你想继续code为基础的移民离开这个标志

在这一点上,你可以开始修改你的模型。每次你创建一个新移民增加迁移它会根据你的模型之前,修改和迁移类将只包含必要的架构更改。

First, I've read these questions/answers:

These all seem to be for EF versions prior to EF5, and my situation doesn't seem to fit these answers. So, let me describe my situation.

  1. My application was originally created using EF4, model first. I designed my database with the GUI designer, and used it to generate my database.
  2. I've been running and collecting data into the database for a few months. I really can't lose this data.
  3. I branched my code, installed EF5 with NuGet, and used EF Power Tools to generate my model from my database by right-clicking a new class library project, and selecting Entity Framework | Reverse engineer code first.
  4. I was able to smoothly re-reference to my new project, convert my project to use the new DbContext instead of ObjectContext, and removed the EF4 class library that held my old model. The program works great!

Now, I want to try out automatic migrations, which I've had a tiny bit of experience with in Ruby on Rails. Here's what I did:

  1. Ran Enable-Migrations. Had a bit of trouble due to connection strings and which app.config was getting used, but eventually got it. However, this MSDN page says that this should have automatically generated the first migration to get me to the point I'm already at. It didn't.
  2. Ran Add-Migration InitialSchema to accomplish what wasn't automatically done in step 1. This worked.
  3. Added a property to one of my model objects, then attempted to run Add-Migration AddSerialToLogEntries, and was presented with:

Unable to generate an explicit migration because the following explicit migrations are pending: [201307190100268_InitialSchema]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

Attempting to apply the migration on my existing database failed, which isn't surprising.

The other answers I mentioned above basically said that I'm out of luck, but like I said those were for older versions of the Entity Framework. Do I have any options here?

While writing this question, I thought I might be able to use SQL Server Management Studio to export my data to a SQL script, delete the entire database, let EF create it, then run the scripts to get my data back in... I will try that tomorrow when I have time, but I would like to hear if there are other options as I'm not 100% sure that would work and would hate to have any errors in the data inserted in the process.

解决方案

Ran Enable-Migrations. Had a bit of trouble due to connection strings and which app.config was getting used, but eventually got it. However, this MSDN page says that this should have automatically generated the first migration to get me to the point I'm already at. It didn't.

Ran Add-Migration InitialSchema to accomplish what wasn't automatically done in step 1. This worked.

In fact the enable-migrations command creates an initial migration only if your database has already been created with Code-First before in which case the database contains a __MigrationHistory table. If this table does not exist (which is the case when you have an existing database that never has been created before with Code-First) enable-migrations only creates the Configuration class. You have to call add-migration manually then to create the first migration class. So, the behaviour you have seen is expected.

Generally the procedure to prepare an existing database for migrations is the following if you are using EF 5:

  • Call enable-migrations in package manager console. A Migrations folder in your project and a Configuration class will be created.

  • Open the Configuration class and set AutomaticMigrationsEnabled = false in the constructor (if it isn't already by default).

  • In package manager console call

    add-migration -IgnoreChanges InitialSchema
    

    "InitialSchema" is only an example name. You can name it like you want. A <Timestamp>_InitialSchema class will be created that derives from DbMigration. The Up and Down methods in this class are empty due to the -IgnoreChanges flag. Without this flag the class would contain a migration to add your whole model to the database which is not what you want since the existing database already contains the database schema.

  • Run update-database in the package manager console. Because the Up method is empty this update does nothing with your existing schema except it creates the __MigrationHistory table (as a system table in your database) and adds the first record to this table that contains a model hash of your current EF model.

  • Optional last step: If you prefer to work with automatic migrations open the Configuration class and set AutomaticMigrationsEnabled = true in the constructor. If you want to proceed with code-based migrations leave the flag false.

At this point you can start to make changes to your model. Everytime you create a new migration with add-migration it will be based on your model before the modification and the migration class will only contain the necessary schema changes.

这篇关于无法与现有的数据库上运行EF5迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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