efcore: add-migration System.ArgumentNullException: Value cannot be null. Parameter name: key
Executing add-migration, I am getting System.ArgumentNullExceptionL Value cannot be null. Parameter name: key.
I got this after updating VS2017 and installing SDK 2.1.300.
My guess is that there is something amiss in my model somewhere, but would like verification that that would be a good guess, given this exception, or if it’s likely to be something else. Also wondering if anyone has any hints of how to track down the source, since there are no line numbers in the stack trace and none of it references any of my solution’s classes.
Here’s the exception and stack trace:
System.ArgumentNullException: Value cannot be null.
Parameter name: key
at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)
at Microsoft.EntityFrameworkCore.RelationalMetadataExtensions.Relational(IKey key)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.<Add>d__35.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__34`1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.<Add>d__26.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.<DiffCollection>d__50`1.MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Further technical details
EF Core version: 2.0.3 Database Provider: Npgsql.EntityFrameworkCore.PostgreSQL Operating system: Windows Server 2008 R2 IDE: Visual Studio 2017 15.7.3
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 16 (5 by maintainers)
I just found the problem… turns out I was giving it a null value 🤦♂️
Even with non-nullables, sometimes things sneak through, especially when you select entire entities.
The lesson here is probably always to query for individual fields… which, actually, is kind of hard when you want to query for entities by reference rather than by ID.
@reinux I did not get a solution to this at that time (I hope it was fixed in a later patch by EF Team) and I rather moved to SSDT for applying database changes as it is more reliable and can be applied to SQL database out of any order, i.e. If I want I can merge any SQL object change in my release branch from dev branch and simply ask it to apply changes. The SSDT runtime automatically generates difference scripts. And at this point if you need to update data, you can do it via Pre-deployment or Post-deployment scripts. If you use EF Migrations, them post merging desired changes, you have to create a new migration in Release branch! Further if your DBA did some changes on DB directly, then these migrations may fail!!
By the way, if you are still on these versions then you are already out of support, so better go to the latest LTS version now.