ASP.NET Core Gotchas – No. 4 Value cannot be null. Parameter name: connectionString when running dotnet ef migrations add

I was upgrading a quite mature ASP.NET Core 1.0 project to ASP.NET Core 2.0 today and ran into an odd issue which took me a good half hour to track down. After working my way through the various breaking changes to things such as authorisation I was pretty much at the point where my project was compiling again.

My last change was to fix-up the migrations in the project due to changes in the NpgSql project as detailed in their migration documentation. The last step there is to run in a dummy migration to the project. I attempted to run in the following command…

dotnet ef migrations add "dummy migration"

This started to run and then threw an error and dumped out a stack trace. I won’t include it all here but the higher frames are as follows…

StructureMap.Building.StructureMapBuildException: Failure while building 'Lambda: Invoke(value(StructureMap.ContainerExtensions+>c__DisplayClass9_0).descriptor.ImplementationFactory, IContext.GetInstance())', check the inner exception for details
1.) Lambda: Invoke(value(StructureMap.ContainerExtensions+<>c__DisplayClass9_0).descriptor.ImplementationFactory, IContext.GetInstance())
2.) Instance of DbContextOptions<ReportingDbContext> (System.Object)
3.) Container.GetInstance(DbContextOptions<ReportingDbContext>)
4.) Lambda: Invoke(value(StructureMap.ContainerExtensions+<>c__DisplayClass9_0).descriptor.ImplementationFactory, IContext.GetInstance())
5.) Instance of Microsoft.EntityFrameworkCore.DbContextOptions (System.Object)
6.) All registered children for IEnumerable<DbContextOptions>
7.) Instance of IEnumerable<DbContextOptions>
8.) Container.GetInstance(IEnumerable<DbContextOptions>)
---> System.ArgumentNullException: Value cannot be null. Parameter name: connectionString at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName) at Microsoft.EntityFrameworkCore.NpgsqlDbContextOptionsExtensions.UseNpgsql(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 NpgsqlOptionsAction)

The key part of the error is “Value cannot be null. Parameter name: connectionString”.

In principle this would seem like a fairly obvious problem. I must have messed something up when performing the configuration changes as part of the upgrade to ASP.NET Core 2.0. However, if I ran the application from Visual Studio, I could see the database being created and migrated (we have a Database.Migrate() call in our code). So this would suggest that in fact the configuration was working and the connection string was being read in. Odd!

At this stage I resorted to Google and found a few similar sounding issues and StackOverflow posts. However they all seemed to suggest that indeed it was due to bad configuration of some form or another. Eventually I decided to double check my csproj file reference. During the migration I’d used the NuGet package manager to upgrade the packages after first editing my csproj to use netcoreapp2.0.

Here’s an abridged example of the csproj file:

Do you see the issue?

It took me a couple of parses to spot something suspicious. At the bottom the file includes a DotNetCliToolReference to bring in the Entity Framework command line tooling. It was set to version 1.0.0 which didn’t seem quite right given that I was now on 2.0.x of most other Microsoft packages. A quick check on Nuget for the latest version showed that there was a 2.0.1 available.

I edited my csproj to change the version, saved it and tried running my migration command again. Success! This time the migration process ran as expected.

It’s easily missed (or at least it was for me) since I’d relied on the Nuget Package Manager to save me some time when upgrading my packages. This tool reference however is not shown there and is something I needed to manually update. Hopefully this saves other people scratching there heads over this error message in the future!


Have you enjoyed this post and found it useful? If so, please consider supporting me:

Buy me a coffeeBuy me a coffee Donate with PayPal

Steve Gordon

Steve Gordon is a Pluralsight author, 6x Microsoft MVP, and a .NET engineer at Elastic where he maintains the .NET APM agent and related libraries. Steve is passionate about community and all things .NET related, having worked with ASP.NET for over 21 years. Steve enjoys sharing his knowledge through his blog, in videos and by presenting talks at user groups and conferences. Steve is excited to participate in the active .NET community and founded .NET South East, a .NET Meetup group based in Brighton. He enjoys contributing to and maintaining OSS projects. You can find Steve on most social media platforms as @stevejgordon

2 thoughts to “ASP.NET Core Gotchas – No. 4 Value cannot be null. Parameter name: connectionString when running dotnet ef migrations add

    1. Hi Nilesh,

      Suggest you post in StackOverflow with as much information as you can. Ideally, the full stack trace / exception details.

      Thanks,
      Steve

Leave a Reply

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