Skip to content

A temporal foreign key to a generic entity type cannot be diffed from its snapshot #197

Closed

Description

@CaffeinatedCoder

Found by a pre-release sweep of release/5.x @ e563ba1 (lens: round-trip); reproduced in a scratch SnapshotRoundTripTests case, and again by the caller. Present since the temporal foreign key shipped; not a 5.6.0 regression.

Problem

A temporal foreign key whose principal is a generic entity type (Room<int>) cannot be diffed from its snapshot. The declaration stores the principal as typeof(TPrincipal).FullName (NpgsqlTemporalConstraintExtensions.cs:110), the CLR form …+Room`1[[System.Int32, System.Private.CoreLib, Version=10.0.0.0, …]]. ResolveEntityType (NpgsqlComplexIndexMigrationsModelDiffer.cs:1706) matches it against e.Name (EF's display name, …+Room<int>) or e.ClrType.FullName. The live model matches through ClrType; a snapshot's property-bag type has ClrType == Dictionary<string, object>, so nothing matches and BuildForeignKeyDescriptors throws.

The first migrations add passes, because there is no snapshot yet. Every later migrations add, has-pending-model-changes, and Migrate()'s pending-changes check throw, even with no change:

System.InvalidOperationException: Could not resolve temporal foreign key principal entity 'EFCore.ComplexIndexes.Tests.SnapshotRoundTripTests+GRoom`1[[System.Int32, System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' from dependent entity '…+GBooking<int>'.
   at NpgsqlComplexIndexMigrationsModelDiffer.BuildForeignKeyDescriptors(...)

It also hid the snapshot path of #187/#188's tests, which use Room<TKey>/Booking<TKey>.

Change

Resolve the stored name in either form: also match an entity type whose EF name equals the stored CLR name with its generic arguments rendered as EF names them (`1[[T, asm]] → <T>). Stored names cannot change, since snapshots hold the FullName form. Optionally store Metadata.Name for new declarations and keep the FullName match for old snapshots. The stored form also carries Version=10.0.0.0, which a runtime major upgrade would change.

Test

SnapshotRoundTripTests: a temporal FK to Room<int>, diffed from its compiled snapshot against the unchanged model, yields no operations. Plus TemporalForeignKeyOrderingTests' scenarios from a snapshot source.

Activity

  1. added this to the 5.6.0 milestone on Oct 23, 2026
  2. added
    bug
    severity/medium
    package/postgresql
    area/constraints
    area/migrations
    on Oct 23, 2026
  3. added a commit that references this issue on Oct 23, 2026
  4. CaffeinatedCoder commented on Oct 23, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Fixed on release/5.x by #199, shipping in 5.6.0. Closed by hand: Closes acts only on merges into main.

    ResolveEntityType now also matches the stored CLR FullName converted to EF Core's entity-type name (EntityTypeNames.FromClrFullName, EF Core's own display rule), so a temporal foreign key to a generic principal such as Room<int> diffs from its snapshot. Stored names are unchanged.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/constraintsarea/migrationsbugpackage/postgresqlseverity/medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions