FIX: Object reference not set to an instance of object Add-Migration

Visual Studio, Add-Migration & parental leave… & Errors…

I was working on a new feature. I had been working on the feature for several weeks and was now ready to add my latest changes to our DTAP.

When I had a parental leave coming up in a few days (wife was pregnant and I’m off for 15 weeks after birth), and I needed to finish everything before then.

I was feeling a bit rushed and decided to push my changes to the DTAP environment as quickly as possible so that my colleagues could continue working on the feature in my absence.

One of the first steps was Add-Migration and that’s where things went wrong. I got the below error message:

System.NullReferenceException: Object reference not set to an instance of an object

I probably did everything too fast in the last week and didn’t pay attention so I got this error.


Let’s fix System.NullReferenceException: Object reference not set to an instance of an object with Add-Migration

Quick fix is without screenshots, the fix with screenshots is with… well yeah screenshots. Both of them contain the same steps.

I use Visual Studio to resolve this issue.


Quick fix for System.NullReferenceException: Object reference not set to an instance of an object

  • Open Solution Explorer in Visual Studio & go to your Migrations directory
  • Delete YourSolutionNameSQLContextModelSnapshot.cs
  • Open Package Manager Console and run Add-Migration with a temporarily migration
  • Delete the newly created migration manually (do not use Remove-Migration)
  • You can now add new migrations again

Fix with screenshots for System.NullReferenceException

Open the Solution Explorer in Visual Studio
Go to your Migrations directory
Search for YourSolutionNameSQLContextModelSnapshot.cs

System.NullReferenceException: Object reference not set to an instance of an object
System.NullReferenceException: Object reference not set to an instance of an object

Right-click YourSolutionNameSQLContextModelSnapshot.cs and Delete the file

System.NullReferenceException: Object reference not set to an instance of an object
Object reference not set to an instance of an object

Open Package Manager Console and run a temporarily Add-Migration
You can use the below as an example

Add-Migration 'TemporarilyMigration'

Build started...
Build succeeded.

Now go to your Migrations directory again and delete the newly created Migration manually

System.NullReferenceException: Object reference not set to an instance of an object
System.NullReferenceException

You should be able to add new migrations again with Add-Migration.


Baby tax

Sieb Fedde Wijdenes, born on 26-01-2023
Sieb Fedde Wijdenes, born on 26-01-2023

Published by

Bas Wijdenes

My name is Bas Wijdenes and I work as a PowerShell DevOps Engineer. In my spare time I write about interesting stuff that I encounter during my work.

Leave a Reply

Your email address will not be published. Required fields are marked *