Skip to content

Add IComparable functionality to ValueComparer to support key ordering #23775

Open

Description

Description

The issue was discovered when migrating an existing project from EF Core 3.1 to EF Core 5.0. The project uses EF Core to work with a PostgreSQL database through Npgsql.EntityFrameworkCore.PostgreSQL provider.

The issue boils down to: If a CLR entity has a property of type System.Net.IPAddress (which is mapped to inet PostgreSQL data type), and there's a unique index defined for this property, an attempt to create a DB migration that includes the CLR entity will fail.

I don't know whether the issue is caused by the EF Core or by the Npgsql, so I'm creating the bug in the EF Core repository first. If the problem is not in the EF Core, I'll create a bug in Npgsql repository.

Steps to Reproduce

  1. Open the minimal sample project IpAddressIndex.zip in Visual Studio.
  2. Open Package Manager Console.
  3. Execute Add-Migration Initial -Verbose in the console.

The command produces the following error:

Property 'Site.Address' cannot be used as a key because it has type 'IPAddress' which does not implement 'IComparable', 'IComparable' or 'IStructuralComparable'. Use 'HasConversion' in 'OnModelCreating' to wrap 'IPAddress' with a type that can be compared

Full command output: CommandOutput.txt

Version Information:

  • EF Core version: 5.0.1
  • Database provider: Npgsql.EntityFrameworkCore.PostgreSQL, v. 5.0.1
  • Target framework: .NET Core 3.1, .NET 5
  • Operating system: Windows 10 Enterprise 64-bit, v. 1903
  • IDE: Visual Studio Professional 2019, v 16.8.3

The issue can be reproduced for the sample project both when it targets .NET Core 3.1 and when it targets .NET 5, provided that the versions of the NuGet packages in the .csproj file do not change. The issue cannot be reproduced if versions EF Core 3.x and Npgsql 3.x packages are used. The .csproj file in the sample project contains a commented out section that can be used to test that.

The issue cannot be reproduced if the index for the property is declared as non-unique.

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions