使用EF 4.3运行Update-Database时出错 [英] Error when running Update-Database with EF 4.3

查看:218
本文介绍了使用EF 4.3运行Update-Database时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是,当运行Update-Database命令时,我收到此错误:

>

由于目标数据库是使用早于EF 4.3的Code First版本创建的,并且不包含迁移历史记录表,因此无法构建下一个迁移。要开始对此数据库使用迁移,请确保当前模型与目标数据库兼容,并执行迁移更新过程。 (在Visual Studio中,您可以使用Package Manager Console中的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


解决方案

您运行启用迁移,脚本可能尚未创建初始迁移,特别是如果您的模型与数据库不匹配,或者如果您的 DbContext class在另一个项目中定义。



我不知道将迁移添加到现有的4.3之前的数据库中的正确方法是,但最简单的方法是转储您的数据库。删除其中的表...



确保Migrations文件夹中的配置文件具有正确的上下文类型,然后添加迁移初始。一个大类将被建立,代表您当前的模型。



现在更新数据库将运行而不抱怨。 / p>




如果您有数据库中关心的数据,您可能会欺骗并添加 __ MigrationHistory 表,迁移过程创建到数据库的先前备份。删除 EdmMetadata ,迁移不应该更明智。



希望有人知道迁移是如何添加到现有的EF代码第一个数据库将有更顺畅的升级步骤?


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

解决方案

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?

这篇关于使用EF 4.3运行Update-Database时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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