From 702b8828f9f273e51d10f07b9281c2cd885f919f Mon Sep 17 00:00:00 2001 From: DJ Kunkel Date: Wed, 15 Apr 2020 18:30:53 -0500 Subject: [PATCH] Support FromSql when using FromRawSql from EF --- .../LinqToDBForEFToolsImplDefault.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFToolsImplDefault.cs b/Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFToolsImplDefault.cs index 746bc88..edaaa06 100644 --- a/Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFToolsImplDefault.cs +++ b/Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFToolsImplDefault.cs @@ -494,6 +494,9 @@ namespace LinqToDB.EntityFrameworkCore static readonly MethodInfo GetTableMethodInfo = MemberHelper.MethodOf(dc => dc.GetTable()).GetGenericMethodDefinition(); + static readonly MethodInfo FromSqlOnQueryableMethodInfo = + typeof(RelationalQueryableExtensions).GetMethods(BindingFlags.Static|BindingFlags.NonPublic).Single(x => x.Name == "FromSqlOnQueryable").GetGenericMethodDefinition(); + static readonly MethodInfo LoadWithMethodInfo = MemberHelper.MethodOf(() => LinqExtensions.LoadWith(null, null)).GetGenericMethodDefinition(); static readonly MethodInfo WhereMethodInfo = @@ -527,6 +530,9 @@ namespace LinqToDB.EntityFrameworkCore static readonly MethodInfo L2DBProperty = typeof(Sql).GetMethod(nameof(Sql.Property)).GetGenericMethodDefinition(); + static readonly MethodInfo + L2DBFromSqlMethodInfo = MemberHelper.MethodOf(() => DataExtensions.FromSql(null,new Common.RawSqlString(), null)).GetGenericMethodDefinition(); + static readonly ConstructorInfo DataParameterConstructor = MemberHelper.ConstructorOf(() => new DataParameter("", "", DataType.Undefined, "")); public static Expression Unwrap(Expression ex) @@ -855,6 +861,15 @@ namespace LinqToDB.EntityFrameworkCore break; } + if (generic == FromSqlOnQueryableMethodInfo) + { + //convert the arguments from the FromSqlOnQueryable method from EF, to a L2DB FromSql call + return Expression.Call(null, L2DBFromSqlMethodInfo.MakeGenericMethod(methodCall.Method.GetGenericArguments()[0]), + Expression.Constant(dc), + Expression.Constant(new Common.RawSqlString((string)EvaluateExpression(methodCall.Arguments[1]))), + methodCall.Arguments[2]); + } + if (typeof(IQueryable<>).IsSameOrParentOf(methodCall.Type)) { // Invoking function to evaluate EF's Subquery located in function -- 2.25.1.windows.1