Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 6, 2025

When Entity Framework Core encounters circular dependencies during SaveChanges(), it attempts to generate a helpful error message that includes details about the entities involved. However, when sensitive data logging is enabled, a NullReferenceException can occur in the CommandBatchPreparer.FormatValues method, which hides the actual circular dependency error from users.

The issue occurs because the FormatValues method assumes the values parameter is never null, but in certain edge cases (particularly with circular dependencies), the value factory methods (CreateDependentKeyValue, CreateKeyValue, CreateIndexValue) can return null. When this happens, accessing values[i] throws a NullReferenceException that masks the real error.

Example of the problem:

Before this fix, users would see:

Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.FormatValues(Object[] values, IReadOnlyList1 columns, IReadOnlyModificationCommand dependentCommand, StringBuilder builder)

After this fix, users will see the actual error:

System.InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved: 'Entity [Added] ForeignKeyConstraint { 'ForeignKey' } <- RelatedEntity [Modified] <- Index { 'UniqueColumn' } Entity [Added]To show additional information call 'DbContextOptionsBuilder.EnableSensitiveDataLogging'.'.

Solution:

Added a null check for the values parameter in the FormatValues method. When values is null, the method now outputs "NULL" for each column instead of attempting to access the array, allowing the real circular dependency error to be properly displayed to users.

The fix is minimal and surgical - it only adds a null check without changing any existing behavior when values is non-null.

Fixes #35665.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…values is null

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Sensitive Data Logging causes NullReferenceException hiding real issue Fix NullReferenceException in CommandBatchPreparer.FormatValues hiding circular dependency errors Aug 6, 2025
Copilot AI requested a review from AndriySvyryd August 6, 2025 00:30
@AndriySvyryd AndriySvyryd marked this pull request as ready for review August 6, 2025 01:21
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner August 6, 2025 01:21
Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndriySvyryd any idea how this didn't trigger an NRT warning/error?

@AndriySvyryd
Copy link
Member

@AndriySvyryd any idea how this didn't trigger an NRT warning/error?

Because it was suppressed 🤦

@AndriySvyryd AndriySvyryd merged commit 98d7b71 into main Aug 6, 2025
6 checks passed
@AndriySvyryd AndriySvyryd deleted the copilot/fix-35665 branch August 6, 2025 21: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.

Sensitive Data Logging causes NullReferenceException hiding real issue

3 participants