Skip to content

Fix parameter collections of nullable structs #3408

Closed as duplicate
Closed as duplicate

Description

@roji

See PrimitiveCollectionsQueryNpgsqlTest.{Parameter_collection_of_nullable_structs_Contains_nullable_struct_with_nullable_comparer,Parameter_collection_of_structs_Contains_nullable_struct_with_nullable_comparer}.

Considering backporting.

Activity

  1. added this to the 10.0.0 milestone on Dec 15, 2024
  2. self-assigned this
    on Dec 15, 2024
  3. added theissue type on Jun 14, 2025
  4. modified the milestones: 10.0.0, 11.0.0 on Nov 22, 2025
  5. roji commented on Oct 17, 2026

    @roji
    MemberAuthor

    AI Triage

    The below is an AI-generated analysis and may contain inaccuracies.

    Confirmed bug, already fixed on current main. The two tests mentioned in this issue cover distinct failures, both of which can be reproduced during query compilation with ToQueryString(); no database or seed data is needed.

    Results

    These are standalone console-program results. "Pass" means SQL generation completed, not that a database query was executed.

    Provider / version List<WrappedId?> containing null List<WrappedId> against nullable property .Value
    Npgsql 7.0.18 Pass InvalidCastException: Object must implement IConvertible
    Npgsql 8.0.11 Missing JSON reader/writer constructor Conversion-lambda argument type mismatch
    Npgsql 9.0.0 and 9.0.4 Missing JSON reader/writer constructor Conversion-lambda argument type mismatch
    Npgsql 10.0.0 Missing JSON reader/writer constructor Conversion-lambda argument type mismatch
    Npgsql 10.0.3 Pass Conversion-lambda argument type mismatch
    Npgsql main at e420326e Pass Pass
    SQL Server 7.0.18 and 8.0.11 Pass Pass
    SQL Server 9.0.0 and 9.0.4 Pass Nullable/non-nullable value-comparer coercion failure
    SQL Server 10.0.0 and 10.0.3 Pass Pass

    Released-package comparisons used .NET 10.0.8; current Npgsql source used the repository's .NET 11 RC2 SDK and EF Core 11.0.0-rc.2.26504.105.

    Regression assessment: The nullable-list case works in Npgsql 7.0.18 but fails in 8.0.11, so this predates EF/Npgsql 9; the exact first failing 8.x release was not bisected. The non-nullable-list case already fails on Npgsql 7.0.18, so it is not a newly introduced 9.0 regression either. SQL Server has a separate confirmed 8-to-9 regression in the second case.

    Fixes and related issues

    • The nullable-list failure is the missing-constructor/JSON element reader-writer mismatch fixed by #3843, which also enabled Parameter_collection_of_nullable_structs_Contains_nullable_struct_with_nullable_comparer. It was backported to 10.0.3, matching the result above. Related duplicate reports: #3842 and #3716.
    • The non-nullable-list failure is in NpgsqlArrayConverter: the collection supplies WrappedId, but the element conversion expression accepts WrappedId?. The inner exception is ArgumentException: Expression of type 'WrappedId' cannot be used for parameter of type 'System.Nullable<WrappedId>'. This is #3919, fixed by #3965, which also enabled Parameter_collection_of_structs_Contains_nullable_struct_with_nullable_comparer. Backports are already merged for 10.0 (#3967), 9.0 (#3968), and 8.0 (#3969). The tested 10.0.3 release still fails this case.
    • Upstream, dotnet/efcore#35117 is an exact match for the SQL Server 9 non-nullable-list/value-comparer failure. It was fixed for EF 10 by dotnet/efcore#35235; the upstream discussion explicitly declined a 9.0 backport in favor of using a non-nullable value converter. dotnet/efcore#36873 is a related comparer-type mismatch report, but uses enum/int types rather than nullable struct converters and was closed without a reproducer.

    The SQL Server 9 overlap does not explain away the Npgsql bugs: Npgsql still fails with EF 10, where SQL Server passes. This issue belongs in the provider repository rather than needing to be reopened upstream.

    Recommendation: Close this issue as resolved on main by #3843 and #3965. The requested non-nullable conversion backports have already been merged; that does not establish that the nullable-list scenario is fixed on the 8.0/9.0 branches.

    A workaround on affected releases is to define the converter over WrappedId and int, rather than WrappedId? and int?; EF handles nulls around ordinary converters.

    minimal repro

    Create a console project with the following project file and Program.cs. Both failures reproduce with provider 9.0.0. Run dotnet run for the nullable-list case, and dotnet run -- non-nullable for the other case.

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net10.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.0" />
      </ItemGroup>
    </Project>
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
    using Microsoft.Extensions.Logging;
    
    await using var context = new TestContext();
    
    if (args.Contains("non-nullable"))
    {
        var values = new List<WrappedId> { new(22), new(33) };
        _ = context.Set<PrimitiveCollectionsEntity>()
            .Where(c => values.Contains(c.NullableWrappedIdWithNullableComparer!.Value))
            .ToQueryString();
    }
    else
    {
        var values = new List<WrappedId?> { null, new(22) };
        _ = context.Set<PrimitiveCollectionsEntity>()
            .Where(c => values.Contains(c.NullableWrappedIdWithNullableComparer))
            .ToQueryString();
    }
    
    public class TestContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseNpgsql("Host=localhost;Database=issue3408")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
            => modelBuilder.Entity<PrimitiveCollectionsEntity>()
                .Property(e => e.NullableWrappedIdWithNullableComparer)
                .HasConversion<NullableWrappedIdConverter>();
    }
    
    public class NullableWrappedIdConverter() : ValueConverter<WrappedId?, int?>(
        id => id == null ? null : id.Value.Value,
        value => value == null ? null : new WrappedId(value.Value));
    
    public class PrimitiveCollectionsEntity
    {
        public int Id { get; set; }
        public WrappedId? NullableWrappedIdWithNullableComparer { get; set; }
    }
    
    public readonly record struct WrappedId(int Value);

    To repeat the SQL Server comparison, replace the package with Microsoft.EntityFrameworkCore.SqlServer at the same EF version and replace UseNpgsql(...) with UseSqlServer("Server=localhost;Database=issue3408;Integrated Security=true;TrustServerCertificate=true"). No server is contacted by either variant.

  6. roji commented on Oct 17, 2026

    @roji
    MemberAuthor

    Duplicate of #3843 and #3965

  7. removed this from the 11.0.0 milestone on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions