Debugging Entity Framework Seed methods

In Entity Framework projects, sometimes you need to debug your seed methods. This can seem a little tricky, since the code tends to run in the Package Manager Console;

pmc

And it’s a fire-and-forget kind of deal. Any runtime errors get swallowed. However, you can debug into your own EF code by;

1) Find the project that contains your DbContext and Configuration.cs file, and set it as your startup project.

2) Open the properties page for the project.

3) Set the external program to the EF migator exe. For EF 6.1, it’s at

($mySourceDirectory)\packages\EntityFramework.6.1.0\tools\migrate.exe

4) Set the command line arguments to point to the name of your EF assembly, the bin folder for that EF assembly, and the web.config or app.config which contains the connection string for the DB you’ll be updating, like so;

MyProject.EntityFramework.dll /startupConfigurationFile="..\..\..\MyWebsite\Web.config" /startupDirectory="full\path\to\MyProjectEntityFramework\bin\Debug"

5) Set breakpoints and press F5!

Advertisements

2 thoughts on “Debugging Entity Framework Seed methods

  1. I’ve got as far as Step #2.

    In Step #3: What do you mean by “Set the external program to the EF migator exe”? Where?How? What?

    In Step #4: You say “Set the command line arguments”. Is that re-build, or post-build?

    In Step #5: Will this give proper feedback in the Package Manager Console whein I run “Update-Database -Verbose”?

    • Hi – right-click the project, choose properties, and go to the debugging tab. That has boxes for the external program and runtime arguments, so that deals with step 3 and 4.

      As to running the powershell command; that powershell command just invokes migrate.exe, the app you set in step 3, so could probably get verbose output with a command-line switch, which you would add to the arguments listed for Step 4. Eg, it will be something like /verbose, added to the end of the args. When you debug it should write the scripts to the console window that appears.

      I can’t check the args right now because I’m in my phone but you can get them by running migrate.exe /?.

      (https://msdn.microsoft.com/en-gb/data/jj618307.aspx)

      Hope that helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s