C# – How to reset database migrations using EF code first

cef-code-firstentity-frameworkentity-framework-migrationsnet

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?

Best Answer

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'