0

I had the following code working in my local environment with EF Core 2.2.6:

public static class GuidFunctions
{
    public static bool IsGreaterThan(this Guid left, Guid right) => left.CompareTo(right) > 0;
    public static bool IsGreaterThanOrEqual(this Guid left, Guid right) => left.CompareTo(right) >= 0;
    public static bool IsLessThan(this Guid left, Guid right) => left.CompareTo(right) < 0;
    public static bool IsLessThanOrEqual(this Guid left, Guid right) => left.CompareTo(right) <= 0;
    public static void Register(ModelBuilder modelBuilder)
    {
        RegisterFunction(modelBuilder, nameof(IsGreaterThan), ExpressionType.GreaterThan);
        RegisterFunction(modelBuilder, nameof(IsGreaterThanOrEqual), ExpressionType.GreaterThanOrEqual);
        RegisterFunction(modelBuilder, nameof(IsLessThan), ExpressionType.LessThan);
        RegisterFunction(modelBuilder, nameof(IsLessThanOrEqual), ExpressionType.LessThanOrEqual);
    }
    static void RegisterFunction(ModelBuilder modelBuilder, string name, ExpressionType type)
    {
        var method = typeof(GuidFunctions).GetMethod(name, new[] { typeof(Guid), typeof(Guid) });
        modelBuilder.HasDbFunction(method).HasTranslation(parameters =>
        {
            var left = parameters.ElementAt(0);
            var right = parameters.ElementAt(1);
            return Expression.MakeBinary(type, left, right, false, method);
        });
    }
}

Full code and source article can be found here: https://entityframeworkcore.com/en/knowledge-base/54920200/entity-framework

After upgrading to EF Core 3.0, it is now throwing this error:

Cannot implicitly convert type 'System.Linq.Expressions.BinaryExpression' to 'Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlExpression'

While checking the methods signature for EF Core v2.2.6 and EF Core v3.0 I could notice that the HasTranslation function type was changed from Expression to SqlExpression, making the returned type incompatible (BinaryExpression returned by Expression.MakeBinary).

EF Core v2.2.6 HasTranslation(Func,Expression>)

EF Core v3.0 HasTranslation(Func,SqlExpression>)

Is there any workaround to have this solution working again with EF Core 3.0?

  • Isn't that just a consequence of this breaking change: LINQ queries are no longer evaluated on the client (https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#linq-queries-are-no-longer-evaluated-on-the-client) ? – David Browne - Microsoft Oct 29 '19 at 22:06
  • See the updated answer of the post marked as duplicate (which is the actual "source article" you are referring to). – Ivan Stoev Oct 30 '19 at 01:39
  • 1
    Thanks Ivan, that solves my question. – Antonio Pinedo Oct 30 '19 at 01:50

0 Answers0