Skip to content

NullReferenceException in SqlServerAnnotationCodeGenerator when scaffolding columns with named default constraints and CLR default values #37175

@collinbarrett

Description

@collinbarrett

Bug description

When scaffolding a database with dotnet ef dbcontext scaffold, a NullReferenceException occurs in SqlServerAnnotationCodeGenerator.GenerateFluentApiCalls for columns that have both:

  • A named default constraint (e.g., DF_TableName_ColumnName)
  • A default value equal to the CLR default (e.g., DEFAULT ((0)) for int)

This appears related to PR #36067 (named default constraint support). Issue #36567 claims to fix this scenario and was tagged for the 10.0.0 milestone, but the bug reproduces in the released 10.0.0 stable version.

Your code

Create a table with a named default constraint where the default value equals the CLR default:

CREATE TABLE TestTable (
    Id INT NOT NULL PRIMARY KEY,
    Value INT NOT NULL CONSTRAINT DF_TestTable_Value DEFAULT ((0))
);

Scaffold the database:

dotnet ef dbcontext scaffold "Server=myserver;Database=mydb;..." Microsoft.EntityFrameworkCore.SqlServer --output-dir Models --context TestContext --force

Result: Scaffolding fails with NullReferenceException

Possible root cause: In SqlServerAnnotationCodeGenerator.GenerateFluentApiCalls (v10.0.0, line 282):

  1. Base scaffolder removes DefaultValue annotation because 0 equals CLR default for int
  2. Then attempts to remove DefaultValue again (no-op since already removed)
  3. Then accesses defaultValueAnnotation.Value to generate fluent API code
  4. defaultValueAnnotation is null → NullReferenceException

This is a different bug than #36567, which crashed when REMOVING the constraint name annotation.
This bug crashes when ACCESSING the default value annotation to generate code.

Why This is a Regression

Issue #36567 was marked as fixed in PR #36576 for the 10.0.0 milestone, but the fix only addressed one scenario.

The fix in #36576 didn't account for the case where BOTH conditions are true:

This causes the null reference when the SQL Server-specific code assumes the DefaultValue annotation still exists.

Stack traces

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.SqlServer.Design.Internal.SqlServerAnnotationCodeGenerator.GenerateFluentApiCalls(IProperty property, IDictionary`2 annotations)
   at Microsoft.EntityFrameworkCore.Design.IAnnotationCodeGenerator.GenerateFluentApiCallsInternal(IAnnotatable annotatable, IDictionary`2 annotations)
   at Microsoft.EntityFrameworkCore.ScaffoldingModelExtensions.GenerateAnnotations(IAnnotatable annotatable, Dictionary`2 annotations, IAnnotationCodeGenerator annotationCodeGenerator, Boolean isHandledByDataAnnotations)
   at Microsoft.EntityFrameworkCore.ScaffoldingModelExtensions.GetFluentApiCalls(IProperty property, IAnnotationCodeGenerator annotationCodeGenerator)
   at Microsoft.VisualStudio.TextTemplating.GeneratedTextTransformation.TransformText()

Verbose output

Microsoft.EntityFrameworkCore.Design.OperationException: Processing 'DbContext.t4' failed.
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.TextTemplatingModelGenerator.HandleErrors(TextTemplatingHost host)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.TextTemplatingModelGenerator.GenerateModel(IModel model, ModelCodeGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(...)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(...)
   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)
Processing 'DbContext.t4' failed.

EF Core version

10.0.0

Database provider

Microsoft.EntityFrameworkCore.SqlServer 10.0.0
Microsoft.EntityFrameworkCore.Design 10.0.0

Target framework

.NET 10.0

Operating system

Windows 11

IDE

Command line (dotnet ef)

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions