Search code examples
c#entity-frameworkvisual-studio-2015code-firstentity-framework-migrations

Migrating Entity Framework without Migration Files


I am working on a project which is using Entity Framework code first for its data structure. It was created with code first, but was never migrated again and only has its initial migration data stored. Since then, the database has been modified directly through server explorer in VS2015.

There is no migration information about any changes and the database has critical information which I cannot lose.

Which brings me to my Questions.

If I create a new migration and update the database from it, will it wipe all changes which were not recorded in migrations and still leave the changes which were made as well?


Solution

  • The details of your question is a bit sketchy, but I will make some assumptions in order to help you along. Please correct where I am wrong.

    I assume that you want to keep the data which resulted from the changes which were effected directly to the database, but you do not want to keep the changes that was effected to the database - in other words: keep the data but not the datastructures.

    My advice is as follows

    • Always perform a full backup of your database when you are about to do something you are uncertain about.
    • If you can identify the tables you want to update, you can always use the SELECT INTO statement to create a quick backup of the specific tables only. These tables will not be removed when you do a EF database migration unless you explicitly script the deletion.
    • You can build the SELECT INTO statement into your EF migration via the Sql() method, or you can manually run the command against the database.

    More information:

    • Click here to learn about EF code first migrations in general
    • Click here for a comprehensive code first migration reference