locked
How will you restore a column using Entity Framework Code First, that has been manually dropped from the database? RRS feed

  • Question

  • User-1095454647 posted

    I replied that I would have to rely on the error log and then generate a sql script from previously created migrations  and run it on database. But he was not satisfied. He said that would create script for whole of the migration but he wants only one column that has been dropped to be reinstated.  Next he asked me if I have heard of Up-streaming and down-streaming in entity framework. I have been looking on internet I could not find anything of that sort related to entity framework.  I am not sure if the two questions were interrelated. They sure seemed to be. Is there really anything such as "Up-streaming" in entity framework code first approach? If not how would you deal with the scenario ? 

    Saturday, March 9, 2019 8:39 AM

All replies

  • User475983607 posted

    Code first migrations have an "Up" method and a "Down" method.  The UP adds to the migration and the Down undoes a migration.  Simply open any of your migration files and take a look.

    Saturday, March 9, 2019 12:56 PM
  • User-1095454647 posted

    I am aware of "Up" and "Down" methods. But how is that going to help you find out which column has been dropped? How are you going to restore the column(only that column and nothing else)  with that? If you write a new migration that would be manual work same as creating a new column with sql script.

    Saturday, March 9, 2019 3:28 PM
  • User475983607 posted

    Arby360

    I am aware of "Up" and "Down" methods. But how is that going to help you find out which column has been dropped? How are you going to restore the column(only that column and nothing else)  with that? If you write a new migration that would be manual work same as creating a new column with sql script.

    Okay so maybe I made a few assumptions.  I assumed you are using source control and have an established migration strategy. If you do not have a clear migration strategy or you are not following a standard process then I'm not sure how to answer this question. Migrations are like a timeline that always moves forward.  There are times during development where migrations are added and undo several times until the migration works as expected.  But, eventually, the migration becomes history and part of the source control.  

    It sounds like you are missing the migration history.  This points to a process bug.

    Saturday, March 9, 2019 4:07 PM
  • User-1095454647 posted

    The question that he had asked was what will you do to restore the deleted column( that included finding out what has been deleted) if someone dropped it manually from database. Ideally no one would do that. Everyone is supposed to be using migration up and down methods to make changes to the database right ? But what if someone from your team "accidentally" drops a column manually just to cause problems. How will you find out which column has been dropped and how will you restore it with EF? 
    Even if you are able to recognize from error that columns "abc" has been dropped.  And if you don't have a migration that has Up and Down method exclusively to add and roll back  column "abc"  what are you going to do? Honestly speaking I think it is a weird question. I don't think there is any other solution to this problem. But I have posted it here just so if there is a solution to this problem I would very much like to know.

    Sunday, March 10, 2019 5:40 AM
  • User-1095454647 posted

    The question that he had asked was what will you do to restore the deleted column( that included finding out what has been deleted) if someone dropped it manually from database. Ideally no one would do that. Everyone is supposed to be using migration up and down methods to make changes to the database right ? But what if someone from your team "accidentally" drops a column manually just to cause problems. How will you find out which column has been dropped and how will you restore it with EF? 
    Even if you are able to recognize from error that columns "abc" has been dropped.  And if you don't have a migration that has Up and Down method exclusively to add and roll back  column "abc"  what are you going to do? Honestly speaking I think it is a weird question. I don't think there is any other solution to this problem. But I have posted it here just so if there is a solution to this problem I would very much like to know.

    Sunday, March 10, 2019 8:13 AM
  • User753101303 posted

    Hi,

    Asking about a question interview in a forum usually leads nowhere. There is no way to really know what the interviewer wanted to ear about as questions are often theorical and anyway this is likely supposed to be a discussion rather than just a question/response game.

    More likely his intent was to find out if you know about EF migration.

    From a  practical point of view you'll likely want to restore a backup side by side as beyond cheking changes and recreating the column, you'll need also to add back missing data...

    Sunday, March 10, 2019 10:28 AM