Spodworld

a collection of technical fixes and other randon stuff

How to debug Seed data in MVC Entity framework project

As database migrations and seeding data becomes more code based and more complex , you may find you have problems debugging some of this code.
For instance if you are using package manager to run the update-database command, you may get the following errors:


c# - Validation failed for one or more entities while saving changes to SQL Server Database

Debugging Package Manager Console Update-Database Seed Method

A first chance exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll

If this is the case, i found the following solution helpful....

  1. Open up another version of Visual Studio
  2. In the new instance of visual studio: open up the file for editing and insert a break point where you suspect a problem is occurring.
  3. Go to the Debug menu and use the attach to process method, to attach to your existing copy of Visual Studio.
  4. In your originalinstance of visual studio go to the package manager console and run "update-database".
  5. When your seed data code is run, you will now hit a break point.
Loading