EF 7迁移到现有数据库 [英] EF 7 Migration to Existing Database

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

问题描述

我正在使用ASP.Net 5和EF7进行Web项目。

I am working on a web project using ASP.Net 5 and EF7.

我已将所有表从现有数据库导入到项目中的模型中。但是,我在迁移方面遇到了问题。

I have imported all the tables from existing database onto my models in my project. However, I am having problems in regards with migrations.

我创建了初始迁移,对特定实体进行了一些更改,根据所做的更改创建了另一个迁移,现在要在数据库上应用更改。

I have created the initial migration, made some changes on particular entity, create another migration following the changes I have made and now want to apply the changes on the database.

在下面运行以下命令后:

After running this command below:

dnx ef数据库更新[Migration]

dnx试图对所有已存在的实体应用初始迁移数据库,这将导致以下错误:

the dnx is trying to apply the 'initial' migration with all the entities which are already in database and this causes an error as below:


{数据库中已经有一个名为['EntityName']的对象。 }

{ There is already an object named ['EntityName'] in the database. }

能否请您提供有关如何在现有数据库上进行迁移的建议?

Can you please advise on how to do the migration on the existing database?

感谢
Saeed

Thanks Saeed

推荐答案

在EF6中,您将使用进行迁移-IgnoreChanges 标志,它将在没有任何 Up()代码的情况下拍摄模型的快照。如此处所示。

In EF6 you would run a migration with the -IgnoreChanges flag and it would take a snapshot of the model without any Up() code. This is missing from EF 7(EF Core) as indicated here.

目前的解决方法是从迁移的 Up()代码中删除或注释掉现有数据库对象的代码,然后进行更新-数据库。随后的迁移将仅包括更改。

The workaround for now is delete or comment out the code for existing database objects from the Up() code of the migration and then update-database. Subsequent migrations will then include only the changes.

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

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