Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaffold-DbContext from package manager console is giving a System.NullReferenceException

I have started to face this issue just recently. I'm using the Visual Studio 2022 Preview latest. I made a few simple changes to the DB and used the validation tool in dbForge Studio 2022 for SQL Server to validate all objects. The DB is valid.

When I try to update my context, I get the following output.

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.

I get a successful build, but it immediately fails with the error. I'd like to research this, but no other log windows show any errors, and I cannot figure out what is causing this.

like image 959
Hassan Gulzar Avatar asked Feb 11 '26 08:02

Hassan Gulzar


1 Answers

It happens because of missing PK.

You have 2 options to manage this problem:

  1. Discover the table that don't have PK by using the script (in 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

and add them PK

  1. Generate EF without the tables that don't have PK by using this script (in SQL)
 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
)

then you can Scaffold DbContext from selected tables of an existing database

like image 113
Nan fish Avatar answered Feb 13 '26 23:02

Nan fish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!