19

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?

EluciusFTW
  • 2,565
  • 6
  • 42
  • 59
  • 1
    Does this help: http://stackoverflow.com/a/26567885/1336590 ? Basically, run `Seed` through the application and not in the PM console. – Corak Mar 04 '15 at 15:35
  • See this blog: http://blogs.msdn.com/b/rickandy/archive/2013/02/12/seeding-and-debugging-entity-framework-ef-dbs.aspx – Colin Mar 04 '15 at 17:15

2 Answers2

44

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();
Stephen Reindl
  • 5,659
  • 2
  • 34
  • 38
  • It was working view hours ago ... this is a cached version; http://webcache.googleusercontent.com/search?q=cache:http://patrickdesjardins.com/blog/how-to-debug-entity-framework-migration-seeding&strip=1 – Stephen Reindl Mar 05 '15 at 10:50
  • Here is another article I found looking for this same question: http://blog.theodybrothers.com/2015/09/debugging-your-seed-method-when-running.html – CesarD May 03 '17 at 18:37
1

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.

Mike Cole
  • 14,474
  • 28
  • 114
  • 194