How to Debug Entity Framework Migration Errors

Prerequisite

In this article, I assume you have a working knowledge of the Entity Framework ORM and its migrations feature. If you don’t, take a look through Entity Framework documentation and its migrations section before continuing.

Introduction

If you’ve ever worked with the Entity Framework ORM for any length of time, then you’ve likely worked with its migrations feature. And more than likely, you’ve had to debug one (or many) of your migrations when trying to run an update-database through the package manager console. Unfortunately, the errors displayed in the Entity Framework can be extremely cryptic. For example, one error I ran into recently was this:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Unfortunately, there is no way to view the EntityValidationErrors property when you run your migrations through the Package Manager Console. So, the only information you’ve got is a stack trace and a cryptic error message. Thanks Entity Framework!

So how do we view the error details? Run the migrations through code! By running our migrations through code, we can use Visual Studio’s (or any other IDE’s) debugging tools to drill down deeper into our errors.

How to Run a Migration Through Code in Entity Framework 6

If you’re using Entity Framework 6 in your project, then running a migration through code is very simple. All you need to do is instantiate the DbMigrator class and call its Update method. This has the same effect as running update-database in the package manager console.

using System.Data.Entity.Migrations;
var configuration = new Configuration(); // Your configuration class
var migrator = new DbMigrator(configuration);
migrator.Update(); // Same effect as running update-database

One thing to note is that the Configuration class is the class that gets generated when you first run enable-migrations in your project. However, you could pass a different class to the DbMigrator constructor if the class is or inherits from the DbMigrationsConfiguration class.

I haven’t tested it, but this method should also work for Entity Framework 4 and 5.

How to Run a Migration Through Code in Entity Framework 2.0

If you’re using Entity Framework Core 2.0 (or greater), then running a migration through code is even simpler than in Entity Framework 6. Simply get an instance of your DbContext and call the Migrate method on its Database property.

using Microsoft.EntityFrameworkCore;
var context = new ApplicationDbContext(null); // Use your DbContext here
context.Database.Migrate(); // Same effect as running update-database

Summary

Debugging Entity Framework migrations through the package manager console can be very painful since all it provides you is a stack trace and a (sometimes) cryptic error message. But by running your migrations programmatically, you can take advantage of your IDE’s debugging tools as you would any other error!

Warning

This method of debugging has worked well for me in the past, but I want to stress that this is not perfect. You must modify your application’s codebase to get this to work, which could lead to unintended side effects. So, use this method sparingly!

If you have a different (or better) idea on how to debug Entity Framework migrations, please leave a comment below!

comment

Comments

arrow_back

Previous

How to Debug Entity Framework Migration Errors

Next

Solving the "We were unable to load Disqus" Error
arrow_forward