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

asp.net-core c# entity-framework-core

Question

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.

1
1
10/23/2019 10:56:13 AM

Accepted Answer

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.

2
10/25/2019 9:20:10 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow