Skip to content

NullReferenceException in EntityTypeConfiguration.t4 template with UseT4Split on .NET 10 / EF Core 10.0.3 #3319

@HonzaZajic298

Description

@HonzaZajic298

Bug description / exact reproduction steps

When using T4 Split templates (UseT4: true, UseT4Split: true) with .NET 10 project, the reverse engineering fails with NullReferenceException in Initialize() method.
The issue is specifically in EntityTypeConfiguration.t4 template. When I use only UseT4 without splitting, the problem won't occur.

Steps to reproduce:

  1. Create a new .NET 10 class library project
  2. Add Microsoft.EntityFrameworkCore.SqlServer (10.0.3) and Microsoft.EntityFrameworkCore.Design (10.0.3)
  3. Configure efpt.config.json with:
    "UseT4": true,
    "UseT4Split": true
  4. Run "Reverse Engineer" from EF Core Power Tools
  5. Select any tables from the database

Your code

-- ============================================
-- SQL Script to reproduce EF Core Power Tools bug
-- Issue: NullReferenceException in EntityTypeConfiguration.t4 
--        with UseT4Split on .NET 10 / EF Core 10.0.3
-- ============================================

-- Create test database
CREATE DATABASE EFCorePowerToolsTest;
GO

USE EFCorePowerToolsTest;
GO

-- ============================================
-- Table: PSW_ITEMS
-- ============================================
CREATE TABLE [dbo].[PSW_ITEMS] (
    [ID] INT NOT NULL,
    [ItemName] NVARCHAR(30) NOT NULL,
    [ItemLabel] INT NOT NULL,
    CONSTRAINT [PK_PSW_ITEMS] PRIMARY KEY CLUSTERED ([ID])
);
GO

-- ============================================
-- Table: PSW_ENTRY_POINTS
-- ============================================
CREATE TABLE [dbo].[PSW_ENTRY_POINTS] (
    [IDEntryPoint] INT NOT NULL,
    [EntryPointName] NVARCHAR(30) NOT NULL,
    [DefaultLabelID] INT NULL,
    [Comment] NVARCHAR(500) NULL,
    CONSTRAINT [PK_PSW_ENTRY_POINTS] PRIMARY KEY CLUSTERED ([IDEntryPoint])
);
GO

-- ============================================
-- Table: PSW_ITEMS_ENTRY_POINTS (junction table)
-- ============================================
CREATE TABLE [dbo].[PSW_ITEMS_ENTRY_POINTS] (
    [ID] INT IDENTITY(1,1) NOT NULL,
    [ItemID] INT NULL,
    [EntryPointID] INT NULL,
    CONSTRAINT [PK_PSW_ITEMS_ENTRY_POINTS] PRIMARY KEY CLUSTERED ([ID]),
    CONSTRAINT [FK_PSW_ITEMS_ENTRY_POINTS__ItemID__PSW_ITEMS] 
        FOREIGN KEY ([ItemID]) REFERENCES [dbo].[PSW_ITEMS] ([ID]),
    CONSTRAINT [FK_PSW_ITEMS_ENTRY_POINTS__EntryPointID__PSW_ENTRY_POINTS] 
        FOREIGN KEY ([EntryPointID]) REFERENCES [dbo].[PSW_ENTRY_POINTS] ([IDEntryPoint])
);
GO

-- ============================================
-- Insert sample data (optional)
-- ============================================
INSERT INTO [dbo].[PSW_ITEMS] ([ID], [ItemName], [ItemLabel]) VALUES
(1, 'Item1', 100),
(2, 'Item2', 200);

INSERT INTO [dbo].[PSW_ENTRY_POINTS] ([IDEntryPoint], [EntryPointName], [DefaultLabelID], [Comment]) VALUES
(1, 'EntryPoint1', 100, 'Test entry point 1'),
(2, 'EntryPoint2', NULL, NULL);

INSERT INTO [dbo].[PSW_ITEMS_ENTRY_POINTS] ([ItemID], [EntryPointID]) VALUES
(1, 1),
(1, 2),
(2, 1);
GO

-- ============================================
-- Verify tables created
-- ============================================
SELECT 'Tables created successfully' AS Result;
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';
GO

Stack traces

error : Error running transform: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.VisualStudio.TextTemplating55f740b0.GeneratedTextTransformation.Initialize()
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Mono.TextTemplating.CompiledTemplate.TemplateProcessor.CreateAndProcess(ITextTemplatingEngineHost host, CompiledAssemblyData templateAssemblyData, String templateAssemblyFile, String fullName, CultureInfo culture, String[] referencedAssemblyFiles) in /_/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.TemplateExecutor.cs:line 77

Microsoft.EntityFrameworkCore.Design.OperationException: Processing 'C:\SAW_Dev\TAW\SAW_COMMON\EntityData2\CodeTemplates\EFCore\EntityTypeConfiguration.t4' failed.
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.TextTemplatingModelGenerator.HandleErrors(TextTemplatingEngineHost host)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.TextTemplatingModelGenerator.GenerateModel(IModel model, ModelCodeGenerationOptions options)
   at RevEng.Core.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions, Boolean removeNullableBoolDefaults, Boolean excludeNavigations, Boolean dbContextOnly, Boolean entitiesOnly, Boolean useSchemaFolders, Boolean useSchemaNamespaces) in D:\a\EFCorePowerTools\EFCorePowerTools\src\Core\RevEng.Core.80\ReverseEngineerScaffolder.cs:line 501
   at RevEng.Core.ReverseEngineerScaffolder.GenerateDbContext(ReverseEngineerCommandOptions options, List`1 schemas, String outputContextDir, String modelNamespace, String contextNamespace, String projectPath, String outputPath, String rootNameSpace) in D:\a\EFCorePowerTools\EFCorePowerTools\src\Core\RevEng.Core.80\ReverseEngineerScaffolder.cs:line 93
   at RevEng.Core.ReverseEngineerRunner.GenerateFiles(ReverseEngineerCommandOptions options) in D:\a\EFCorePowerTools\EFCorePowerTools\src\Core\RevEng.Core.80\ReverseEngineerRunner.cs:line 92

EF Core Power Tools version

2.6.1378

EF Core Power Tools CLI version

???

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 10

IDE

Visual Studio 2026 18.3.1

Additional features in use

  • T4 templates
  • SQL Server .dacpac
  • Handlebars template

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions