locked
Mistake using EF code first RRS feed

  • Question

  • User148055990 posted

    Hi

    I have just made what is probably a stupid error using the dotnet ef framework and VS 2019

    I had about 90 tables in my database, each one has been created using an EF migration. I manually added a line of data to one table and noticed that the table ID had jumped from 33 to 1032 whereas I wanted the ID column to have consecutive ID's, so I ran an SQL command to drop the table and then tried to run the relevant migration that created the table in the first place, thinking that it would just re-create the table. 

    The command I ran was dotnet ef database update TableMapping --context SIHGContext.

    What then happend is that every table created after TableMapping was dropped from the DB. Obviously I shouldn't have done what I did but I'm now trying to get all the previous tables re-created without doing any further damage. 

    Can anyone tell me

    a) what I should have done to just run one migration created in the past

    b) how to recreate all the tables that were dropped.

    many thanks

    Saturday, November 30, 2019 11:43 AM

Answers

  • User665608656 posted

    Hi Kiboko,

    According to your description, in fact, when you delete a table, you can re-perform the migration on your current code and update the database.

    If your current class has not been modified, you can execute in the following order to generate the deleted table:

    1. Enable-Migrations
    2. Add-Migration
    3. Update-Database

    You can also refer to the following links :

    How do I force drop and re-create a a selected table in code-first migration?

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 2, 2019 7:18 AM

All replies

  • User665608656 posted

    Hi Kiboko,

    According to your description, in fact, when you delete a table, you can re-perform the migration on your current code and update the database.

    If your current class has not been modified, you can execute in the following order to generate the deleted table:

    1. Enable-Migrations
    2. Add-Migration
    3. Update-Database

    You can also refer to the following links :

    How do I force drop and re-create a a selected table in code-first migration?

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 2, 2019 7:18 AM
  • User148055990 posted

    Thank you Yongqing,

    Can I just run dotnet ef database update --context SIHGContext to recreate the tables that have been dropped? - there were about 40 tables dropped or do I need to recreate a new migration that will include all those tables in it?

    Monday, December 2, 2019 8:05 AM
  • User665608656 posted

    Hi Kiboko,

    Can I just run dotnet ef database update --context SIHGContext to recreate the tables that have been dropped? - there were about 40 tables dropped or do I need to recreate a new migration that will include all those tables in it?

    Since you have created multiple tables, you can first follow the steps below (You can also refer to this link : https://stackoverflow.com/a/16082497):

    1. Manually delete __MigrationHistory table.
    2. Remove all migration files - which are under Migrations - and named like numbers etc. - remove them all,
    3. Rebuild your project containing migrations (and the rest) .
    4. Run Add-Migration Initial again - then Update-Database


    Before executing the Update-Database command at the last step, you need to delete the CreateTable method of all other existing tables in the Up method of the current migration file, and then execute the Update-Database command to succeed.

    Best Regards,

    YongQing.

    Tuesday, December 3, 2019 3:27 AM
  • User148055990 posted

    Thank you again YongQuing,

    I have now resolved the problem. The lost tables were on the hosting server not on my local DB so I just ran all the migrations again one by one and have now recreated all the tables, and fortunately I kept a record of all the data I entered into those tables so ran those SQL commands to repopulate them.
    Thanks very much for your kind help.
    regards
    Tuesday, December 3, 2019 5:15 AM