Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 26, 2025

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Description

Fixes #37175

Scaffolding fails with NullReferenceException when a column has both a named default constraint and a default value equal to the CLR default (e.g., DEFAULT ((0)) for int).

Root cause: SqlServerAnnotationCodeGenerator.GenerateFluentApiCalls 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.

Fix: Changed the Check.DebugAssert to a conditional else if check. When neither DefaultValue nor DefaultValueSql exists, skip generating code for the constraint name instead of throwing.

-- This table definition triggers the bug
CREATE TABLE TestTable (
    Id INT NOT NULL PRIMARY KEY,
    Value INT NOT NULL CONSTRAINT DF_TestTable_Value DEFAULT ((0))
);
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
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.

2 participants