Entity framework manually deleted tables cant be generated from EF migration

前端 未结 4 2054
北恋
北恋 2021-02-20 05:26

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

A B C D E . Now again I have changed some part of code and ra

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-20 05:58

    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. 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 .

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

    3. 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.

提交回复
热议问题