You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(As mentioned in this comment on an older, closed issue #12290, I have moved to create an issue for my specific problem.)
When attempting to use a custom struct with a defined value conversion to and from ulong as a key, performing a FindAsync() query will generate an exception:
System.InvalidOperationException: The binary operator Equal is not defined for the types 'Disqord.Snowflake' and 'Disqord.Snowflake'.
at System.Linq.Expressions.Expression.GetEqualityComparisonOperator(ExpressionTypebinaryType,StringopName,Expressionleft,Expressionright,BooleanliftToNull)
at System.Linq.Expressions.Expression.Equal(Expressionleft,Expressionright,BooleanliftToNull,MethodInfomethod)
at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.BuildPredicate(IReadOnlyList`1keyProperties,ValueBufferkeyValues,ParameterExpressionentityParameter)
at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.BuildLambda(IReadOnlyList`1keyProperties,ValueBufferkeyValues)
at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.FindAsync(Object[]keyValues,CancellationTokencancellationToken)
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.FindAsync(Object[]keyValues)
I am not sure if this is a regression from issue #12290 which was supposedly fixed, or a completely separate issue altogether. If needed, I am happy to test this on non-super-unstable-and-new versions of EF Core. This just happened to be the version I was using.
Steps to reproduce
*Note: This Snowflake struct is a simplified version of this struct from a library I am using. If you believe the full struct would assist in any way for finding a solution you are welcome to use this instead for triage or testing.
Below is a full one-file example project I created to test this. The code does not perform any migrations or raw database operations so for testing, migrations will need to be created and applied to the testing database.
publicreadonlystructSnowflake:IEquatable<ulong>,IEquatable<Snowflake>,IComparable<ulong>,IComparable<Snowflake>{publiculongRawValue{get;}publicSnowflake(ulongrawValue){RawValue=rawValue;}publicboolEquals(ulongother)=>RawValue==other;publicboolEquals(Snowflakeother)=>RawValue==other.RawValue;publicoverrideboolEquals(objectobj){if(objisSnowflakesnowflake)returnsnowflake.RawValue==RawValue;if(objisulongrawValue)returnrawValue==RawValue;returnfalse;}publicoverrideintGetHashCode()=>RawValue.GetHashCode();publicoverridestringToString()=>RawValue.ToString();publicintCompareTo(ulongother)=>RawValue.CompareTo(other);publicintCompareTo(Snowflakeother)=>RawValue.CompareTo(other);publicstaticimplicitoperatorSnowflake(ulongvalue)=>newSnowflake(value);publicstaticimplicitoperatorulong(Snowflakevalue)=>value.RawValue;}publicsealedclassGuild{publicSnowflakeId{get;set;}publicstringName{get;set;}}publicsealedclassSnowflakeValueConverter:ValueConverter<Snowflake,ulong>{publicSnowflakeValueConverter():base(x =>x.RawValue, x =>newSnowflake(x)){}}publicclassGuildContext:DbContext{protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<Guild>(g =>{g.HasKey(x =>x.Id);g.Property(x =>x.Id).HasConversion(newSnowflakeValueConverter());});}publicDbSet<Guild>Guilds{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseNpgsql("YOUR_DATABASE_CONNECTION_STRING");}publicclassProgram{privatestaticreadonlySnowflakeExampleId=132349439506382848;publicstaticasyncTaskMain(){using(varcontext=newGuildContext()){context.Guilds.Add(newGuild{Id=ExampleId,Name="Test"});awaitcontext.SaveChangesAsync();}using(varcontext=newGuildContext()){try{varguild=awaitcontext.Guilds.FindAsync(ExampleId);Console.WriteLine($"Guild found with ID {ExampleId} and name {guild.Name}!");}catch(Exceptionex){Console.WriteLine($"Guild NOT found with ID {ExampleId}.");Console.WriteLine(ex);}Console.ReadKey();}}}
Assuming everything is set up correctly, when Main() is called, it should enter the catch section and complain about the Guild not being found, and printing a similar if not identical exception message to console.
Further technical details
EF Core version: 5.0.0-preview.5.20278.2
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL, version 5.0.0-preview5
Target framework: .NET 5.0 preview (5.0.100-preview.4.20258.7)
Operating system: Windows 10 x64
IDE: Visual Studio 2019 16.7.0 Preview 2.0
The text was updated successfully, but these errors were encountered:
QuantumToasted
changed the title
System.InvalidOperationException about a missing binary Equal operator on custom struct types used as keys
"Binary operator Equal is not defined" on custom struct types used as keys
Jun 22, 2020
(As mentioned in this comment on an older, closed issue #12290, I have moved to create an issue for my specific problem.)
When attempting to use a custom struct with a defined value conversion to and from
ulong
as a key, performing aFindAsync()
query will generate an exception:I am not sure if this is a regression from issue #12290 which was supposedly fixed, or a completely separate issue altogether. If needed, I am happy to test this on non-super-unstable-and-new versions of EF Core. This just happened to be the version I was using.
Steps to reproduce
*Note: This Snowflake struct is a simplified version of this struct from a library I am using. If you believe the full struct would assist in any way for finding a solution you are welcome to use this instead for triage or testing.
Below is a full one-file example project I created to test this. The code does not perform any migrations or raw database operations so for testing, migrations will need to be created and applied to the testing database.
Assuming everything is set up correctly, when
Main()
is called, it should enter the catch section and complain about theGuild
not being found, and printing a similar if not identical exception message to console.Further technical details
EF Core version:
5.0.0-preview.5.20278.2
Database provider:
Npgsql.EntityFrameworkCore.PostgreSQL
, version5.0.0-preview5
Target framework: .NET 5.0 preview (
5.0.100-preview.4.20258.7
)Operating system: Windows 10 x64
IDE: Visual Studio 2019 16.7.0 Preview 2.0
The text was updated successfully, but these errors were encountered: