EF核心迁移错误:对象引用未设置为对象的实例

编程入门 行业动态 更新时间:2024-10-28 07:31:33
本文介绍了EF核心迁移错误:对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

之前,添加迁移对我不起作用,因为迁移和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核心迁移错误:对象引用未设置为对象的实例

本文发布于:2023-11-16 07:11:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1602115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   设置为   实例   核心   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!