Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one set a breakpoint in EF code first migrations seed method?

I am having trouble with something in the Seed method in the Configure.cs for my entity framework 6 code-first migration process. I am running the Update-Database -verbose command in the Package Manager Console, and tried to set breakpoints (in VS studio web express 2013) in the c# code of the Seed method. But even if I put it on the first statement in the method, it is not hit, although the console displays running seed method (and subsequently breaks due to my error)

So can one somehow set breakpoints in the Seed method? If not, what is the best way to debug that code?

like image 942
EluciusFTW Avatar asked Mar 04 '15 15:03

EluciusFTW


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.

What are the migration commands we use with code First approach in Entityframework?

Run Enable-Migrations command in a Package Manager console. This command added two more classes to your project in the Migrations folder. This migration was generated because Code First already created a database for us before we enabled migrations. It allows you to configure how Migrations behave for your context.


2 Answers

It's not possible directly within source code but you can attach the debugger via source code. Please see this link for details:

if (System.Diagnostics.Debugger.IsAttached == false)
   System.Diagnostics.Debugger.Launch();

The other option would be to run the migration via source code as explained above:

var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update();
like image 179
Stephen Reindl Avatar answered Oct 04 '22 23:10

Stephen Reindl


Update-Database runs out of your debugging session so you cannot set a breakpoint. You'll want to run your Seed method elsewhere from within your code, like a dummy method, that you can kick off from within your app.

like image 40
Mike Cole Avatar answered Oct 04 '22 22:10

Mike Cole