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

Most upvoted comments

Added PK solved the problem for me too. This is the query I used to find the table without primary key:

select schema_name(tab.schema_id) as [schema_name], 
    tab.[name] as table_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
order by schema_name(tab.schema_id),
    tab.[name]

🩹 Workaround

You can work around this by using custom templates and adding a null check to DbContext.t4 line 219:

<#= foreignKey.PrincipalToDependent != null ? $"p => p.{foreignKey.PrincipalToDependent.Name}" : "" #>

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.

/****** Object:  Table [dbo].[ScaffoldingTest2]    Script Date: 14/11/2022 8:27:00 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ScaffoldingTest2](
	[Id] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_ScaffoldingTest2] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO


/****** Object:  Table [dbo].[ScaffoldingTest]    Script Date: 14/11/2022 8:27:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ScaffoldingTest](
	[Id] [uniqueidentifier] NOT NULL,
	[Description] [nvarchar](500) NOT NULL,
	[ScaffoldingTest2Id] [uniqueidentifier] NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[ScaffoldingTest]  WITH CHECK ADD  CONSTRAINT [FK_ScaffoldingTest_ScaffoldingTest2] FOREIGN KEY([ScaffoldingTest2Id])
REFERENCES [dbo].[ScaffoldingTest2] ([Id])
GO

ALTER TABLE [dbo].[ScaffoldingTest] CHECK CONSTRAINT [FK_ScaffoldingTest_ScaffoldingTest2]
GO

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)