EF迁移无法生成实体框架手动删除的表 [英] Entity framework manually deleted tables cant be generated from EF migration

本文介绍了EF迁移无法生成实体框架手动删除的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了迁移并创建了数据库和表.例如表格是

I have created migration and created the database and the tables . For example the tables are

A B C D E .现在我再次更改了部分代码并运行了 update-database command .一切都很顺利和美好,表格显示了列.现在不小心我手动删除了两个表 D 和 E.现在,当我尝试使用 update-database 运行迁移时.它运行正常,但不会创建我手动删除的表.我试图删除现有的迁移并重新运行 update-database .它给出了除了两个表之外的错误.'A ,B, C 'bla bla 名称中已经存在一个对象.

A B C D E . Now again I have changed some part of code and ran update-database command . Everything went smooth and nice and the tables showed the columns . Now accidentally I manually deleted one two tables D and E. Now when I try to run the migration with update-database . It runs properly but doesn't create the tables which I deleted manually . I tried to delete the existing migration and re-run update-database . It gives the error that apart from the two tables . There already an object existing in 'A ,B, C ' bla bla name.

知道如何在不删除数据库的情况下摆脱这种情况并使用迁移重新创建已删除的表吗?因为我不想删除数据库,因为它包含其余表中的数据.在这种情况下如何继续,我在数据库中有现有表,并且不小心从 SSMS 的 SQL server 中手动删除了几个表.

Any idea how to get rid of this situation without dropping database and recreate the deleted tables using migration ? Cause i dont want to drop the database as it contains the data in rest of the tables. How to proceed in this situation where I have existing tables in database and accidentally I have manually deleted few tables from SQL server from SSMS .

如何使用迁移重新创建表?

How to recreate the tables again using migration ?

哦,我的实体框架版本是 6.0.2

Oh my entity framework version is 6.0.2

推荐答案

我终于找到了解决方案.它基本上改变了我们如何使用迁移的策略.Migration Add-migration 可能仅检查模型和先前的迁移时间戳 cs 文件.所以直到并且除非我们在 nuget 中提供 update-database 命令.它实际上永远不会知道哪些表已被手动删除.因此,当我们尝试在表上执行一些迁移(如更改)时,上下文.它会导致有问题的迁移,因为该表在数据库中不存在,但迁移假定它已经存在.所以为此有一项手工工作.以下是我们从数据库中手动删除表后的步骤

I finally figured out the solution . Its basically change in strategy how we use migration . The Migration Add-migration only checks the Models and the previous migration timestamp cs files possibly . so until and unless we provide update-database command in nuget . it Never actually gets to know which Tables have got deleted manually . So to the context when we try to perform some migration like alter on the tables . It causes problematic migration since that table doesn't exist on the database but the migration assumes it is already there . So for that there is a manual work . Here are the steps after we have deleted a table manually from the database

  1. 检查实体中存在的模型与数据库表的对应关系.如果我们发现数据库表中存在一些异常,这些异常从数据库中丢失但它作为实体存在,这意味着.它们彼此不同步.所以我们必须为每个找到模型 => 表关系.

  1. Check the models existing in entities with correspondence to the database table . If we have found there are some anomaly in the database table which is missing from database but it exists as entity that means. They both are not in sync with each other. So we have to find the model => Table relation for each .

如果我们已经为所有表创建了初始迁移,则从迁移文件中复制已删除的 createTable 代码.

If we have created initial migration with all tables then copy the deleted createTable code from the migration file .

将其粘贴到最近生成的最后一个迁移文件中.然后生成脚本或运行 update-database command .这将创建已删除的数据库表.但是,没有自动命令可以在所有实体和数据库表之间同步.这是我们必须在迁移中部分手动跟踪的东西.

Paste it into the last recently generated migration file. And then generate the script or run the update-database command . That would create the deleted database table . However there is no automatic command which would both sync between all entities and the database tables . That's something which we have to track partially manual in migration.

这篇关于EF迁移无法生成实体框架手动删除的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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