efcore: NullReferenceException scaffolding when no primary key set
upgraded a dotnet app from 6 to 7 with efcore updated to 7
unable to scaffold
dotnet-ef dbcontext scaffold
Entity Framework Core .NET Command-line Tools 7.0.0
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)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 11
- Comments: 24 (13 by maintainers)
Commits related to this issue
- Scaffolding: Handle FK on keyless entity type Fixes #29516 — committed to bricelam/efcore by bricelam 2 years ago
- Scaffolding: Handle FK on keyless entity type Fixes #29516 — committed to bricelam/efcore by bricelam 2 years ago
- Scaffolding: Handle FK on keyless entity type Fixes #29516 — committed to bricelam/efcore by bricelam 2 years ago
Added PK solved the problem for me too. This is the query I used to find the table without primary key:
🩹 Workaround
You can work around this by using custom templates and adding a null check to DbContext.t4 line 219:
Note from triage: we should prepare a patch for this.
Which provider? Can you share your database schema? Can you share your .csproj?
@Jacko1394 It’s not fixed in 7.0.2. Check the milestone.
Minimum example to replicate the issue. Scaffolding these 2 tables causes the error.
Thanks, I meant “CREATE” scripts for you database schema (tables, foreign keys)