包管理器控制台中的 Scaffold-DbContext 给出了 System.NullReferenceException

Has*_*zar 13 visual-studio-2022 .net-7.0

我最近才开始面对这个问题。我正在使用最新的 Visual Studio 2022 预览版。我对数据库进行了一些简单的更改,并使用 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.
Run Code Online (Sandbox Code Playgroud)

我获得了成功的构建,但它立即失败并出现错误。我想研究这个问题,但没有其他日志窗口显示任何错误,而且我无法弄清楚是什么原因造成的。

Nan*_*ish 10

这是因为失踪而发生的PK

您有 2 个选项来解决此问题:

  1. PK使用脚本(SQL 语言)发现不存在的表:
  select tab.[name]
  from sys.tables tab
  left outer join sys.indexes pk
  on tab.object_id = pk.object_id 
  and pk.is_primary_key = 1
  where pk.object_id is null
Run Code Online (Sandbox Code Playgroud)

并添加它们PK

  1. 使用此脚本(在 SQL 中)生成EF没有的表PK
 SELECT name
 FROM sys.tables
 where name in ( 
    select tab.[name] 
    from sys.tables tab
    left outer join sys.indexes pk
    on tab.object_id = pk.object_id 
    and pk.is_primary_key = 1
    where pk.object_id is not  null
)
Run Code Online (Sandbox Code Playgroud)

然后您可以从现有数据库的选定表中使用 Scaffold DbContext


Has*_*zar 0

经过一番漫无目的的挣扎,我终于决定一一撤消我的模式更改,当我从数据库中删除该表时,脚手架又开始正常工作:

CREATE TABLE [dbo].[LookupImportBrand] (
    [CompanyId] BIGINT          NOT NULL,
    [BranchId]  BIGINT          NOT NULL,
    [Name]      [dbo].[Name250] NOT NULL,
    [Active]    [dbo].[Boolean] NOT NULL,
    CONSTRAINT [FK_LookupImportBrand_LookupImportCompany] FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[LookupImportCompany] ([Id])
);
Run Code Online (Sandbox Code Playgroud)

我无法确定这里的根本原因是什么,但我重新创建了表格,一切都很好。