Clairvoyant
Clairvoyant

Reputation: 91

EF Core migration error: Object reference not set to an instance of an object

Earlier, add migrations was not working for me since the migration and dbcontext were in different assemblies. After I move both of them inside the same assembly, that error doesn't occur anymore, instead, I am getting this new error.

Since there is not much information other than the stack trace, I am unable to find where the problem is.

Command:

dotnet-ef migrations add identity 

Stack trace:

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(String value) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(CreateTableOperation operation, IndentedStringBuilder builder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(String builderName, IReadOnlyList1 operations, IndentedStringBuilder builder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMigration(String migrationNamespace, String migrationName, IReadOnlyList1 upOperations, IReadOnlyList1 downOperations) 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.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_01.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Object reference not set to an instance of an object.

Upvotes: 3

Views: 9847

Answers (2)

Omid Rahimi
Omid Rahimi

Reputation: 477

Please Remove HasComment from DbContext

Upvotes: 0

Muhammad Hannan
Muhammad Hannan

Reputation: 2567

There was an issue with EF core which causes System.NullReferenceException when you Add-Migration.

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

The reason issue occurs is when any of your entity configurations have HasComment in it. It's fixed and will be available via EF core 3.1 Nuget (currently is in preview). For EF core 3.0 the workaround is to remove any HasComment.

Upvotes: 2

Related Questions