【问题标题】:EF Core migration error: Object reference not set to an instance of an objectEF Core 迁移错误:对象引用未设置为对象的实例
【发布时间】:2019-10-23 10:56:13
【问题描述】:

之前,添加迁移对我不起作用,因为迁移和 dbcontext 位于不同的程序集中。在我将它们都移动到同一个程序集中后,该错误不再发生,而是出现了这个新错误。

由于除了堆栈跟踪之外没有太多信息,我无法找到问题所在。

命令:

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(字符串 builderName、IReadOnlyList1 操作、IndentedStringBuilder 构建器) 在 Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMigration(字符串 migrationNamespace, String migrationName, IReadOnlyList1 upOperations, IReadOnlyList1 downOperations) 在 Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(字符串 migrationName, String rootNamespace, String subNamespace, String 语言)在 Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(字符串 名称,字符串 outputDir,字符串 contextType)在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(字符串 名称,字符串 outputDir,字符串 contextType)在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.c__DisplayClass0_0.<.ctor>b__0() 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.c__DisplayClass3_01.b__0() 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作 action) 对象引用未设置为对象的实例。

【问题讨论】:

  • 如果我需要发布更多代码,请告诉我,并指定我应该发布什么代码。
  • 我正在从 VS Code 迁移。
  • 您使用的是哪个版本的 EF?

标签: c# asp.net-core entity-framework-core


【解决方案1】:

有一个带有 EF 核心的 issue,当您 Add-Migration 时会导致 System.NullReferenceException

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

出现问题的原因是当您的任何实体配置中包含 HasComment 时。它已修复,将通过 EF core 3.1 Nuget 提供(目前为预览版)。对于 EF core 3.0,解决方法是删除任何 HasComment

【讨论】:

    【解决方案2】:

    请从 DbContext 中删除 HasComment

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 2011-07-09
      • 2012-08-31
      • 2011-11-21
      • 2012-10-01
      • 1970-01-01
      相关资源
      最近更新 更多