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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With