之前,添加迁移对我不起作用,因为迁移和dbcontext位于不同的程序集中。在将它们都移到同一个装配件中之后,不再出现该错误,而是出现了这个新错误。
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.
命令:
dotnet-ef migrations add identity堆栈跟踪:
System.NullReferenceException:对象引用未设置为对象的实例。在处Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(字符串值)在处Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(CreateTableOperation 操作,IndentedStringBuilder构建器) Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(String builderName,IReadOnlyList1 operation,IndentedStringBuilder builder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator。在处的GenerateMigration(String migrationNamespace,String migrationName,IReadOnlyList1 upOperations, IReadOnlyList1 downOperations)Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName,字符串rootNamespace,字符串subNamespace,处的字符串语言)Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(字符串名称,位于处的字符串outputDir,字符串contextType)Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(字符串名称,位于字符串b处的字符串outputDir,字符串contextType)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)对象引用未设置为对象的实例。
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.
推荐答案
有一个带有EF的 issue Add-Migration 时导致 System.NullReferenceException 的核心。
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.出现问题的原因是,当您的任何实体配置具有 HasComment 。它是固定的,可以通过EF core 3.1 Nuget(当前处于预览状态)使用。对于EF core 3.0,解决方法是删除所有 HasComment 。
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.
更多推荐
EF核心迁移错误:对象引用未设置为对象的实例
发布评论