Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset database migrations using EF code first?

I have a class library project with my DbContext and migration enabled with following config file:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <connectionStrings>
    <add name="DataContext" connectionString="Data Source=Data.sdf" providerName="System.Data.SqlServerCe.4.0" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

Some time before I played with migrations for this project and Get-Migrations command always returns following to me:

PM> Get-Migrations -StartupProjectName "Data"
Retrieving migrations that have been applied to the target database.
201207012104355_Initial
201207012031234_Initial
201207012024250_Initial

The problem is that command always returns these items even if I delete Data.sdf or delete all project and make new one. The only way I can create new database is changing database file name in connection string from Data.sdf to Data1.sdf for example. So how can i reset migration history without changing database name?

like image 274
Volodymyr Dombrovskyi Avatar asked Jul 02 '12 09:07

Volodymyr Dombrovskyi


People also ask

How do I remove migration from code first?

This can be easily done by deleting your Migrations folder and dropping your database; at that point you can create a new initial migration, which will contain your entire current schema. It's also possible to reset all migrations and create a single one without losing your data.


1 Answers

Don't know if this is the official method. But here's how I did it.

  1. Deleted migration file
  2. Deleted matching row from __MigrationHistory

    DELETE FROM __MigrationHistory WHERE MigrationId='201210271944168_AddLogTable'

like image 166
Rick Avatar answered Oct 01 '22 17:10

Rick