简体   繁体   English

EF6代码优先更新数据库,不删除数据

[英]EF6 Code First update-database without deleting data

I'm trying to update my db with changes I've made in my entity models (a few extra table columns), but I don't want to drop & recreate the database as it will delete all my test data. 我正在尝试使用在实体模型中所做的更改(一些额外的表列)来更新数据库,但是我不想删除并重新创建数据库,因为它将删除所有测试数据。 Previously I've done this and used the Seed method to create some basic data, but now I have a lot of data which I'd rather keep. 以前,我已经做到了这一点,并使用Seed方法创建了一些基本数据,但是现在我有很多我希望保留的数据。

I've tried to run update-database but I get the error: 我尝试运行更新数据库,但出现错误:

There is already an object named 'ActivityNoteLines' in the database.

Looking at the migration script I can see that it's trying to create the table ActivityNoteLines. 查看迁移脚本,我可以看到它正在尝试创建表ActivityNoteLines。 Obviously I don't want it to to that. 显然,我不希望如此。 I then created an initial migration using 然后,我使用创建了一个初始迁移

Add-Migration InitialCreate –IgnoreChanges

but now when I run update-database, it thinks there are no changes! 但是现在当我运行update-database时,它认为没有变化!

I'm sure it's possible to do what I want, but I can't seem to get it to work. 我确定可以做我想做的事,但我似乎无法使其工作。 If I have to drop & recreate the db I will but would like to know if anyone has successfully updated AND kept the data. 如果必须删除并重新创建数据库,我将但想知道是否有人成功更新并保留了数据。

The issue you are having is that your Create script in the Migrations folder wants to create all the tables. 您遇到的问题是,“迁移”文件夹中的“创建”脚本想要创建所有表。 When Update runs it looks at the table dbo.MigrationHistory to see which migration scripts have already run. 运行Update时,它会查看表dbo.MigrationHistory以查看已运行的迁移脚本。 Your database doesn't have the create in the table so Update is trying to run it. 您的数据库在表中没有创建,因此Update试图运行它。

You can either try to manually add the entry to the table or edit the create script and erase the contents of the up and down methods. 您可以尝试将条目手动添加到表中,也可以编辑创建脚本并清除up和down方法的内容。 Then when you run update it will run the create script, which will do nothing, and write the entry to the MigrationHistory table. 然后,当您运行update时,它将运行创建脚本,该脚本将不执行任何操作,并将条目写入MigrationHistory表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM