Skip to content

Instantly share code, notes, and snippets.

@diffused
Created November 26, 2013 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diffused/7656467 to your computer and use it in GitHub Desktop.
Save diffused/7656467 to your computer and use it in GitHub Desktop.
Delete all data from DB except the dbo._MigrationHistory table using Entity Framework.
using(var context = new PlayContext())
{
context.Database.ExecuteSqlCommand(@"
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL';
EXEC sp_MSForEachTable 'IF OBJECT_ID(''?'') NOT IN (OBJECT_ID(''[dbo].[__MigrationHistory]'')) DELETE FROM ?';
EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL';
");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment