When using custom T4 templates any compilation errors in the templates cause a NullReferenceException because the HandleErrors is only called after processing the templates, and not right after compilation.
The call should be used after both the compilation step and the processing step, as the template will be null if compilation fails, causing the NullReferenceException instead of a clear compilation error message.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Scaffolding.Internal.TextTemplatingModelGenerator.GenerateModel(IModel model, ModelCodeGenerationOptions options)
at UA.Uniform.Db.Scaffolder.Scaffolding.Internal.TextTemplatingModelGenerator.GenerateModel(IModel model, ModelCodeGenerationOptions options) in D:\pas\ua\ua.uniform\tools\source\UA.Uniform.Db.Scaffolder\Scaffolding\Internal\TextTemplatingModelGenerator.cs:line 21
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)
Bug description
When using custom T4 templates any compilation errors in the templates cause a NullReferenceException because the HandleErrors is only called after processing the templates, and not right after compilation.
The call should be used after both the compilation step and the processing step, as the template will be null if compilation fails, causing the NullReferenceException instead of a clear compilation error message.
Your code
Stack traces
Verbose output
EF Core version
9.0.10
Database provider
not applicable
Target framework
.NET 9.0
Operating system
Windows 11
IDE
CLI and Rider 2025.2.3