Skip to content

fix(sqlserver): give unique complex indexes over nullable columns EF's IS NOT NULL filter (#88) - #89

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/88-sqlserver-nullable-unique
Oct 19, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/88-sqlserver-nullable-unique

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #88

Problem

On SQL Server a unique complex index over nullable columns admitted only one row without a value. EF Core's SqlServerIndexConvention gives its own unique indexes over nullable columns WHERE [c] IS NOT NULL, because SQL Server treats NULLs as equal in a unique index; complex indexes got nothing. On an optional complex property, HasComplexIndex(x => x.Registration!.Vin, isUnique: true) scaffolded CREATE UNIQUE INDEX [ux_complex_vin] ON [cars] ([Registration_Vin]);, the migration applied, and the second car without a registration failed with Cannot insert duplicate key row … The duplicate key value is (<NULL>) (Azure SQL Edge).

Change

  • Core: a protected virtual ImplicitFilter(entityType, storeObject, columns, isUnique, providerAnnotations), consulted when a declaration has no filter, on both sides of the diff. Null in the core.
  • SQL Server: [c] IS NOT NULL AND … over the nullable key columns of a unique, nonclustered index, EF's rule, gated on rendering version ≥ 3.
  • Rendering version 3: a snapshot from an earlier 5.x (version 2, or none) resolves without the filter, so the first migrations add rebuilds each affected index once, the mechanism 5.4.0 introduced. SnapshotRoundTripTests pinned the literal 2; it now reads CurrentRenderingVersion.

Changelogs: a ### Changed answers row for #88 with its Upgrading note (one rebuild per affected index; the snapshot's rendering version moves to 3 on every provider, with no other index change).

Merge note: #69, #71, #73 and #87 add the same ### Changed answers heading to the 5.5.0 entry; whichever lands later needs a trivial changelog rebase.

Tests

  • SqlServerNullableUniqueTests: the filter on single and composite indexes over nullable columns matches EF's native answer; no filter for required columns, a declared filter, or a clustered index; a version-2 snapshot rebuilds exactly the two affected indexes, and the current model diffs clean against itself.
  • Live, on Azure SQL Edge with the target model: without the change the second row fails with a duplicate key; with it both insert.
  • verify-the-guard: with the source reverted, the filter test and the one-time rebuild test fail.
  • Full suite 375/375 locally, integration included.

Side finding, for #83's test-gap list: MigrationHarness.SqlServerSql renders with model: null, which makes EF's generator apply its pre-2.0 legacy filter and hides this class of bug.

🤖 Generated with Claude Code

…s IS NOT NULL filter (#88)

SQL Server treats NULLs as equal in a unique index, which is why EF
Core's SqlServerIndexConvention filters its own unique indexes over
nullable columns with IS NOT NULL. A unique complex index got no such
filter, so on an optional complex property (all columns nullable) the
second row without a value failed with a duplicate key. Reproduced on
Azure SQL Edge with the migration's target model, as database update
renders it; rendering with model: null hides it, because EF's generator
then applies its pre-2.0 legacy filter.

The core differ gains a protected virtual, ImplicitFilter, resolved on
both sides of the diff like the rest of the descriptor. The SQL Server
satellite returns [c] IS NOT NULL AND ... over the nullable key columns
of a unique, nonclustered index declared without a filter, gated on the
model's rendering version, which goes to 3: a snapshot from an earlier
5.x resolves the old way, so existing indexes are rebuilt once instead
of never. The snapshot round-trip test that pinned version 2 now reads
the current version.

Verified by reverting the source: the filter test and the one-time
rebuild test fail. Live on SQL Edge: without the change the second row
fails, with it both insert.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred severity/high Wrong results, crash or failed writes in a realistic scenario package/core Affects EFCore.ComplexIndexes package/sqlserver Affects EFCore.ComplexIndexes.SqlServer area/indexes Complex-property, composite, unique and filtered indexes labels Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 3db7bf0 into main Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/88-sqlserver-nullable-unique branch October 19, 2026 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/indexes Complex-property, composite, unique and filtered indexes bug Something isn't working package/core Affects EFCore.ComplexIndexes package/sqlserver Affects EFCore.ComplexIndexes.SqlServer severity/high Wrong results, crash or failed writes in a realistic scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL Server: a unique complex index over nullable columns admits only one row without a value

1 participant