You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在PMC执行Scaffold-DbContext时触发System.NullReferenceException

NullReferenceException 更新DbContext时触发(Visual Studio 2022 Preview)

问题场景

使用Visual Studio 2022 Preview更新DbContext,编译提示成功后立即抛出System.NullReferenceException,报错堆栈指向EF Core脚手架生成逻辑,无额外日志辅助定位问题。已通过dbForge Studio 2022 for SQL Server验证数据库对象均有效。

报错堆栈:

Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpDbContextGenerator.TransformText()
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpModelGenerator.ProcessTemplate(ITextTransformation transformation)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpModelGenerator.GenerateModel(IModel model, ModelCodeGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.

排查与解决步骤

  • 对齐EF Core版本
    检查项目中EF Core相关NuGet包(如Microsoft.EntityFrameworkCore、Microsoft.EntityFrameworkCore.SqlServer、Microsoft.EntityFrameworkCore.Tools)的版本是否一致,且与Visual Studio Preview内置的工具版本匹配。可通过Package Manager Console执行以下命令查看版本:

    Get-Package | Where-Object {$_.Id -match "Microsoft.EntityFrameworkCore"}
    

    若存在版本差异,统一升级至最新稳定版或预览版对应版本。

  • 排查数据库特殊对象
    即使dbForge验证通过,以下场景仍可能触发脚手架bug:

    • 检查是否存在无主键的表:EF Core脚手架要求表必须定义主键,无主键表会导致生成失败。
    • 检查表/列名称是否包含特殊字符(如空格、中文、非标准符号),尝试临时重命名后重新生成。
    • 若生成范围包含视图或存储过程,尝试排除这类对象,仅生成表来测试是否恢复正常。
  • 缩小生成范围定位问题对象
    不要一次性生成所有表,在更新向导中指定单个表进行生成,逐个测试,定位到触发异常的具体表或对象后,针对性调整该对象的结构。

  • 清理缓存与临时文件

    • 删除项目的bin和obj目录,重新编译项目。
    • 关闭Visual Studio,删除%LOCALAPPDATA%\Microsoft\VisualStudio\<你的Preview版本号>\ComponentModelCache目录下的所有文件,重启VS后重试生成操作。
  • 使用EF Core CLI替代VS向导
    通过命令行执行脚手架命令,可能获取到VS向导隐藏的错误细节:
    Package Manager Console命令:

    Scaffold-DbContext "你的数据库连接字符串" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context YourDbContextName
    

    .NET CLI命令:

    dotnet ef dbcontext scaffold "你的数据库连接字符串" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models --context YourDbContextName
    
  • 测试稳定版Visual Studio
    由于使用的是Preview版本,可能存在兼容性bug。尝试使用Visual Studio 2022稳定版执行相同操作,确认是否为预览版本身的问题。

内容的提问来源于stack exchange,提问作者DoomerDGR8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 14:35:22