Debugging Package Manager Console Update-Database Seed Method

A cleaner solution (I guess this requires EF 6) would IMHO be to call update-database from code:

var configuration = new DbMigrationsConfiguration<TContext>();
var databaseMigrator = new DbMigrator(configuration);
databaseMigrator.Update();

This allows you to debug the Seed method.

You may take this one step further and construct a unit test (or, more precisely, an integration test) that creates an empty test database, applies all EF migrations, runs the Seed method, and drops the test database again:

var configuration = new DbMigrationsConfiguration<TContext>();
Database.Delete("TestDatabaseNameOrConnectionString");

var databaseMigrator = new DbMigrator(configuration);
databaseMigrator.Update();

Database.Delete("TestDatabaseNameOrConnectionString");

But be careful not to run this against your development database!


Here is similar question with a solution that works really well.
It does NOT require Thread.Sleep.
Just Launches the debugger using this code.

Clipped from the answer

if (!System.Diagnostics.Debugger.IsAttached) 
    System.Diagnostics.Debugger.Launch();

The way I solved this was to open a new instance of Visual Studio and then open the same solution in this new instance of Visual Studio. I then attached the debugger in this new instance to the old instance (devenv.exe) while running the update-database command. This allowed me to debug the Seed method.

Just to make sure I didn't miss the breakpoint by not attaching in time I added a Thread.Sleep before the breakpoint.

I hope this helps someone.


If you need to get a specific variable's value, a quick hack is to throw an exception:

throw new Exception(variable);