且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EF迁移无法生成实体框架手动删除的表

更新时间:2023-02-13 16:08:29

我终于找到了解决方案.它基本上改变了我们如何使用迁移的策略.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.