6

We use to get the following error when we try to run add-migration. We use Entity Framework Core 2.1.

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.EntityFrameworkCore.Metadata.Internal.TableMapping.<>c.b__10_0(IEntityType t) at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func2 predicate) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(TableMapping target) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(TableMapping target, DiffContext diffContext)+MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable1 sources, IEnumerable1 targets, DiffContext diffContext, Func4 diff, Func3 add, Func3 remove, Func4[] predicates)+MoveNext() at System.Linq.Enumerable.ConcatIterator1.MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable1 operations, DiffContext diffContext) 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) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Object reference not set to an instance of an object.

1
  • More details required: is this the first migration? If not, what was changed in the mean time? Have there been any manipulations with migrations or manual changes in the database? Migrations have many fail points, just showing an error only is not enough. Commented Jun 11, 2024 at 15:24

4 Answers 4

29

The issue for this is a mismatch between DataContextModelSnapshot file and your entity classes. Usually this happen due to merge conflicts in the DataContextModelSnapshot file.

It's very hard to find the mismatch just by going through the history of the DataContextModelSnapshot file. Therefore the easiest solution for this is to;

  • Delete the Snapshot file.
  • Run Add-Migration 'Migration_Name' (This is going to create the snapshot file from the scratch and also create a new Migration file).
  • Delete the Newly Created Migration file manually.
  • Now you can start adding Add-Migration for the new changes.
Sign up to request clarification or add additional context in comments.

Comments

5

I had a similar issue but found it was because I named a previous migration DateOnly

see https://github.com/dotnet/efcore/issues/26954

1 Comment

Thanks for this. I had the same problem with DateTimeOffset.
0

Be careful with the solution proposed by Tharindu Jayasinghe!Before you delete anything, ensure the state of your development environment.

In my case I deleted two attributes and added three new ones.

col1 // was deleted in class
col2 // was deleted in class

col3 // was added in class
col4 // was added in class
col5 // was added in class

In my migration class I then received the following result:

rename col1, col3
rename col2, col4
add col5

When I then called "dotnet ef database update" I got the exception "Object reference not set to an instance of an object."

So I went and adjusted the migration class by hand:

Up:

drop col1
drop col2

add col3
add col4
add col5

Down:

add col1
add col2

drop col3
drop col4
drop col5

Then I called "dotnet ef database update" again, which ran without any problems! The database was then adjusted as I expected :-)

Comments

-1

Go to your solution explorer and find the Migration folder. Open the sub-files of the migration folder, delete all files and run add Migration.

Nayan Makwana helped me to solve this error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.