efcore icon indicating copy to clipboard operation
efcore copied to clipboard

NullReferenceException when adding migration

Open clement911 opened this issue 1 year ago • 9 comments

EF Core version: 8.0.3 Database provider: Microsoft.EntityFrameworkCore.SqlServer 8.0.3 Target framework: .NET 8.0 Operating system: Windows 11 x64 IDE: VS 2002 17.8

We are getting a NullReferenceException when trying to add a new migration.

Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.ColumnBase`1.Microsoft.EntityFrameworkCore.Metadata.IColumnBase.get_ProviderValueComparer()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IColumn source, IColumn target, DiffContext diffContext)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(ITable source, ITable target, DiffContext diffContext)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+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(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.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)
Object reference not set to an instance of an object.

Repro: https://github.com/clement911/EfNullMigration

clement911 avatar Mar 27 '24 22:03 clement911

@maumar have you had any luck reproducing this issue?

clement911 avatar Apr 05 '24 00:04 clement911

I didn't get a chance to look into it yet. We have a huge backlog of work items at the moment, so response times are longer than usual. I will post my findings as soon as I get to it. Apologies for the delays!

maumar avatar Apr 05 '24 04:04 maumar

I understand. Thanks for the update and I look forward to seeing your findings.

clement911 avatar Apr 05 '24 06:04 clement911

It would be good if you could at least confirm that this is a bug that will be fixed?

clement911 avatar Apr 20 '24 00:04 clement911

I have the same issue. It involves adding a migration for DateOnly. The IsNullable of the stored previous schema incorrectly returns true. The new model returns false. I am still investigating why this previous migration indicates that the column was nullable when this property is unchanged.

JelleHissink avatar Apr 23 '24 07:04 JelleHissink

I also suspected it was related to DateOnly

clement911 avatar Apr 23 '24 08:04 clement911

I tried reproducing my problem in a small new solution howerver I was unable. But after some more investigation about a warning about <DateOnly?> not being valid, I found my issue. It all boils down to having a class named DateOnly in my solution, and thus the DateOnly type referenced in the model that is built not a value type but my custom reference type. Thus nullable and not having any valueconverters.

In my case this class was created by adding a migration named "DateOnly" a while ago.

dotnet ef migrations add DateOnly

I renamed my class and my issue seems resolved.

JelleHissink avatar Apr 23 '24 20:04 JelleHissink

Thank you @JelleHissink , I can confirm that this was the same root cause for us and that renaming the migration class fixes the issue. @maumar are there any unintended consequences to renaming a migration class?

clement911 avatar Apr 24 '24 00:04 clement911

No, I don't see an issue in renaming, according to me it acts only on the attributes, just also make sure you rename the classname in the .Designer.cs too.

JelleHissink avatar Apr 24 '24 05:04 JelleHissink