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

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

问题描述

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

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

A B C D E.现在我再次更改了部分代码,并运行了update-database命令.一切都顺利进行了,桌子上的柱子列了.现在,我不小心手动删除了两个表D and E.现在,当我尝试使用update-database运行迁移时.它可以正常运行,但是不会创建我手动删除的表.我试图删除现有的迁移并重新运行update-database.它给出了除两个表之外的错误. bla名称中已经存在一个对象,名称为'A,B,C'.

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

推荐答案

我终于找到了解决方案.它从根本上改变了我们如何使用迁移的策略.迁移Add-migration仅检查Models和先前的迁移时间戳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. 检查对应于数据库表的实体中存在的模型.如果我们发现数据库表中存在一些异常,该异常从数据库中丢失,但是它作为实体存在,这意味着.它们两者彼此不同步.因此,我们必须为每个模型找到model => Table关系.

  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天全站免登陆