使用 EF 4.3 运行更新数据库时出错

发布于 2025-01-05 09:47:53 字数 1081 浏览 1 评论 0原文

我将一个项目升级到 Entity Framework 4.3 并在该项目上启用了迁移。

但是,在运行 Update-Database 命令时出现此错误:

无法构建下一次迁移,因为目标数据库是使用早于 EF 4.3 的 Code First 版本创建的,并且不包含迁移历史记录表。要开始针对此数据库使用迁移,请确保当前模型与目标数据库兼容并执行迁移更新过程。 (在 Visual Studio 中,您可以使用包管理器控制台中的 Update-Database 命令来执行迁移更新过程)。

基本上,它告诉我运行给我错误的相同命令(更新数据库)。

有什么想法吗?


这不完全是一种“有趣”的方式,但我让应用程序创建一个新数据库,该数据库创建一个名为“__MigrationHistory”的系统表。然后,我运行以下脚本在旧数据库上创建该表。我还创建了一个脚本来将新数据库中存在的一行复制到旧数据库中。

如果来自 Microsoft 或社区的人知道更有效的方法,请在此处发帖!


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[__MigrationHistory](
    [MigrationId] [nvarchar](255) NOT NULL,
    [CreatedOn] [datetime] NOT NULL,
    [Model] [varbinary](max) NOT NULL,
    [ProductVersion] [nvarchar](32) NOT NULL,
 CONSTRAINT [PK___MigrationHistory] PRIMARY KEY CLUSTERED 
(
    [MigrationId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,         ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

I upgraded a project to Entity Framework 4.3 and enabled migrations on the project.

However, I get this error when running the Update-Database command:

Cannot scaffold the next migration because the target database was created with a version of Code First earlier than EF 4.3 and does not contain the migrations history table. To start using migrations against this database, ensure the current model is compatible with the target database and execute the migrations Update process. (In Visual Studio you can use the Update-Database command from Package Manager Console to execute the migrations Update process).

Basically, it is telling me to run the same command (Update-Database) that is giving me the error.

Any ideas?


Not exactly a "fun" way to do it, but I let the app create a new database, which creates a system table called "__MigrationHistory". I then ran the following script to create that table on my old database. I also created a script to copy the one row that existed in the new database to the old database.

If someone from Microsoft or community knows a more efficient way of doing this, please post here!


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[__MigrationHistory](
    [MigrationId] [nvarchar](255) NOT NULL,
    [CreatedOn] [datetime] NOT NULL,
    [Model] [varbinary](max) NOT NULL,
    [ProductVersion] [nvarchar](32) NOT NULL,
 CONSTRAINT [PK___MigrationHistory] PRIMARY KEY CLUSTERED 
(
    [MigrationId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,         ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不喜欢何必死缠烂打 2025-01-12 09:47:53

当您运行 Enable-Migrations 时,脚本可能尚未创建初始迁移,特别是如果您的模型与数据库不匹配,或者您的 DbContext 类是在不同的项目。

我不确定将迁移添加到现有 4.3 之前的数据库的“正确方法”是什么,但最简单的方法是转储数据库。将表放入其中...

确保 Migrations 文件夹中的配置文件具有正确的上下文类型,然后添加Add-Migration Initial。将构建一个大类,代表您当前的模型。

现在 Update-Database 将运行而不会出现任何抱怨。


如果数据库中有您关心的数据,您可以作弊并将迁移过程创建的 __MigrationHistory 表添加到数据库的先前备份中。删除 EdmMetadata 并且迁移不会变得更明智。

希望真正了解如何将迁移添加到现有 EF Code First 数据库的人能够有更顺利的升级步骤吗?

When you ran Enable-Migrations, the scripts may have not created an initial migration, especially if your model was mismatched to the database, or if your DbContext class is defined in a different project.

I'm not sure what the "right way" to add migrations to an existing pre-4.3 database is, but the easiest way is to dump your database. Drop the tables in it...

Make sure the Configuration file in the Migrations folder has the right context type, and then Add-Migration Initial. A big class will get built, representing your current model.

Now Update-Database will run without complaining.


If you have data that you care about in the db, you may be able to cheat and add the __MigrationHistory table that the migrations process creates to a previous backup of your database. Delete the EdmMetadata and migrations shouldn't be any wiser.

Hopefully someone who actually knows how migrations were intended to be added to an existing EF Code First database will have smoother upgrade steps?

﹏雨一样淡蓝的深情 2025-01-12 09:47:53

我刚刚在一个数据库上遇到了这个问题,我“认为”已经是 4.3 但不是......

我找到了答案 博客文章

这是基本步骤。

  1. 不要对模型进行任何更改,真的,不要!
  2. 添加初始迁移。
  3. 更新数据库(全部)。

现在您可以继续正常工作了。

要点如下:

不要更改模型。

如果更改了,则需要取消这些更改。哎呀。我刚刚注释掉了我的更改。幸运的是,对我来说,变化仍然相当小。当然,无论如何,你都是在小块地做事情,对吧。 :-)

添加初始迁移。

Add-Migration "InitialModel" -IgnoreChanges

在 up 脚本中添加以下内容:

public override void Up()    
{        
  Sql("DROP TABLE EdmMetadata");    
}

添加删除表不是必需的,但 4.3 及以上版本不使用 EdmMetadata,所以也可以。

更新数据库(全部)

Update-Database

您是否有用于该数据库的其他服务器?测试、分期、生产?请务必对所有这些都执行最后一步。您可以等到完成所有迁移工作后,再对其他服务器执行此操作。

现在,继续正常进行。进行更改并遵循正常的 Add-Migration & 操作。迁移的 Update-Database 步骤。

I just ran into this on a database I "thought" was already 4.3 but was not....

I found the answer with this blog post.

Here's the basic steps.

  1. Don't make any changes to the model, really, don't!
  2. Add an initial migration.
  3. Update the database (all of them).

Now you can go about your normal business.

Here's the nitty gritty:

Don't change the model.

If you have, you need to back those out. Ugggg. I just commented out my changes. Luckily, for me, the changes were still fairly small. Of course you are doing things in small chunks anyway right. :-)

Add an initial migration.

Add-Migration "InitialModel" -IgnoreChanges

In the up script add the following:

public override void Up()    
{        
  Sql("DROP TABLE EdmMetadata");    
}

Adding the drop table is not necessary but 4.3 and on don't use EdmMetadata so, might as well.

Update the Database (all of them)

Update-Database

Do you have other servers for this database? Testing, Staging, Production? Be sure to do this final step for all of them as well. You can wait until you are done with all of your migration work before doing this to the other servers as well.

Now, continue on as normal. Make your changes and follow the normal Add-Migration & Update-Database steps for migrations.

睫毛溺水了 2025-01-12 09:47:53

感谢您的提问和回答。我已经完成了以下操作(上面的建议的组合)。

如何使用 数据 和 Code First 模型从 EF 4.3 之前的版本迁移:

  1. 注释掉与当前数据相比所做的所有更改(我需要删除表添加)。
  2. 备份数据库
  3. 删除表 EdmMetadata
  4. 使用上面的脚本添加表 __MigrationHistory
  5. 运行 Add-Migration "InitialModel"
  6. 删除除 __MigrationHistory< 之外的所有表/code> table
  7. 运行 Update-database
  8. 使用新添加的数据为 __MigrationHistory 生成脚本。添加删除表 EdmMetadata
  9. 恢复数据库并运行此脚本。
  10. 取回代码中的更改。
  11. 运行 Add-Migration YOURNAMEFORNEWCHANGES
  12. 运行 Update-Database

我将旧数据库的数据更新为 EF 6。希望有帮助!

Thanks for the question and answers. I've done the following (a composition of advises above).

How to migrate from pre-EF 4.3 with data and Code First model:

  1. Comment out all changes done comparing to current data (I needed to remove table adding).
  2. Backup database
  3. Remove table EdmMetadata
  4. Add table __MigrationHistory with the script above
  5. Run Add-Migration "InitialModel"
  6. Drop all tables except the __MigrationHistory table
  7. Run Update-database
  8. Generate script for __MigrationHistory with the newly added data. Add there Drop table EdmMetadata.
  9. Restore database and run this script.
  10. Get back the changes in the code.
  11. Run Add-Migration YOURNAMEFORNEWCHANGES
  12. Run Update-Database

And I have the old database with the data updated to EF 6. Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文