Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Package Manager Console Update-Database Seed Method

I wanted to debug the Seed() method in my Entity Framework database configuration class when I run Update-Database from the Package Manager Console but didn't know how to do it. I wanted to share the solution with others in case they have the same issue.

like image 921
Sachin Kainth Avatar asked May 23 '13 15:05

Sachin Kainth


People also ask

What is the purpose of the seed method in migrations?

Seed method is used to initialized the database tables with some starting data. Whenever you run the migration and update the database it will run the seed method. Mostly it is used during the testing phase where you often need to recreate the database and populate database tables with sample data.


3 Answers

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();
like image 156
EthR Avatar answered Oct 17 '22 19:10

EthR


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.

like image 20
Sachin Kainth Avatar answered Oct 17 '22 18:10

Sachin Kainth


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

throw new Exception(variable);
like image 14
cederlof Avatar answered Oct 17 '22 20:10

cederlof