Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 26, 2025

Fixes #37175

Description
When scaffolding from 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) and a default value equal to the CLR default (e.g., DEFAULT ((0)) for int).

The SqlServerAnnotationCodeGenerator.GenerateFluentApiCalls method assumed that if DefaultConstraintName exists, either DefaultValue or DefaultValueSql must also exist. However, the base scaffolding logic in ScaffoldingModelExtensions.GenerateAnnotations can remove these annotations when the default value equals the CLR default, leaving only DefaultConstraintName.

Customer impact
Scaffolding fails completely when the database contains tables with named default constraints where the default value equals the CLR default (e.g., DEFAULT ((0)) for int). No workaround exists other than removing the named default constraint from the database or avoiding scaffolding those tables.

How found
Customer reported on EF 10

Regression
Yes. This is a regression from EF Core 9.0, introduced by PR #36067 as previously the constraint name was just ignored. #36567 attempted to fix a similar scenario but didn't account for this case.

Testing
Test added

Risk
Low. The fix simply skips generating fluent API code for the constraint name instead of throwing.

Original prompt

Fix #37175. Start by creating a test that fails with the same or similar exception


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix test to reproduce exception for issue 37175 Fix NullReferenceException in SqlServerAnnotationCodeGenerator when scaffolding columns with named default constraints and CLR default values Nov 26, 2025
Copilot AI requested a review from AndriySvyryd November 26, 2025 02:06
Copilot finished work on behalf of AndriySvyryd November 26, 2025 02:06
@AndriySvyryd
Copy link
Member

@copilot Update progress by rewriting the PR summary using this template:

Fixes #{number}

**Description**
{Issue description}

**Customer impact**
{How does the reported issue affects customer's app? Are there workarounds?}

**How found**
{Was it customer reported or found during verification? How many customers are affected?}

**Regression**
{Is it a regression from a released version? Which one?}

**Testing**
{How the changes were tested}

**Risk**
{Low/Medium/High, argument why}

@AndriySvyryd AndriySvyryd changed the title Fix NullReferenceException in SqlServerAnnotationCodeGenerator when scaffolding columns with named default constraints and CLR default values [release/10.0] Fix NullReferenceException when scaffolding named default constraints Dec 2, 2025
Copy link
Contributor Author

Copilot AI commented Dec 2, 2025

@copilot Update progress by rewriting the PR summary using this template:

...

Updated the PR description with the requested template format.

Copilot AI changed the title [release/10.0] Fix NullReferenceException when scaffolding named default constraints Fix NullReferenceException in SqlServerAnnotationCodeGenerator when scaffolding columns with named default constraints and CLR default values Dec 2, 2025
Copilot finished work on behalf of AndriySvyryd December 2, 2025 00:22
@AndriySvyryd AndriySvyryd changed the title Fix NullReferenceException in SqlServerAnnotationCodeGenerator when scaffolding columns with named default constraints and CLR default values [release/10.0] Fix NullReferenceException when scaffolding named default constraints Dec 2, 2025
@AndriySvyryd AndriySvyryd marked this pull request as ready for review December 2, 2025 02:14
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner December 2, 2025 02:14
@AndriySvyryd AndriySvyryd requested a review from artl93 December 5, 2025 02:14
@AndriySvyryd
Copy link
Member

cc @artl93

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants