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?