Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 4ed7939

Browse files
committed
Merged changes from 5.x
1 parent 06ba6b9 commit 4ed7939

File tree

6 files changed

+117
-359
lines changed

6 files changed

+117
-359
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Linq.Expressions;
66
using System.Reflection;
7+
using System.Runtime.CompilerServices;
78
using LinqToDB.Expressions;
89
using LinqToDB.Reflection;
910
using Microsoft.EntityFrameworkCore;
@@ -60,19 +61,12 @@ public T[] GetAttributes<T>(Type type, bool inherit = true) where T : Attribute
6061
var contextProp = Expression.Property(Expression.Convert(dcParam, typeof(LinqToDBForEFToolsDataConnection)), "Context");
6162
var filterBody = filter.Body.Transform(e =>
6263
{
63-
switch (e)
64+
if (typeof(DbContext).IsSameOrParentOf(e.Type))
6465
{
65-
case ConstantExpression cnt:
66-
{
67-
if (typeof(DbContext).IsSameOrParentOf(cnt.Type))
68-
{
69-
Expression newExpr = contextProp;
70-
if (newExpr.Type != cnt.Type)
71-
newExpr = Expression.Convert(newExpr, cnt.Type);
72-
return newExpr;
73-
}
74-
break;
75-
}
66+
Expression newExpr = contextProp;
67+
if (newExpr.Type != e.Type)
68+
newExpr = Expression.Convert(newExpr, e.Type);
69+
return newExpr;
7670
}
7771

7872
return e;
@@ -314,6 +308,24 @@ public override void Print(ExpressionPrinter expressionPrinter)
314308
{
315309
expressionPrinter.Print(Expression);
316310
}
311+
312+
protected bool Equals(SqlTransparentExpression other)
313+
{
314+
return ReferenceEquals(this, other);
315+
}
316+
317+
public override bool Equals(object obj)
318+
{
319+
if (ReferenceEquals(null, obj)) return false;
320+
if (ReferenceEquals(this, obj)) return true;
321+
if (obj.GetType() != this.GetType()) return false;
322+
return Equals((SqlTransparentExpression) obj);
323+
}
324+
325+
public override int GetHashCode()
326+
{
327+
return RuntimeHelpers.GetHashCode(this);
328+
}
317329
}
318330

319331
private Sql.ExpressionAttribute? GetDbFunctionFromMethodCall(Type type, MethodInfo methodInfo)
@@ -390,7 +402,32 @@ private static EFCoreExpressionAttribute ConvertToExpressionAttribute(MemberInfo
390402
{
391403
string PrepareExpressionText(Expression? expr)
392404
{
393-
var idx = Array.IndexOf(parameters, expr);
405+
var idx = -1;
406+
407+
for (var index = 0; index < parameters.Length; index++)
408+
{
409+
var param = parameters[index];
410+
var found = ReferenceEquals(expr, param);
411+
if (!found)
412+
{
413+
if (param is SqlTransparentExpression transparent)
414+
{
415+
if (transparent.Expression is ConstantExpression constantExpr &&
416+
expr is SqlConstantExpression sqlConstantExpr)
417+
{
418+
//found = sqlConstantExpr.Value.Equals(constantExpr.Value);
419+
found = true;
420+
}
421+
}
422+
}
423+
424+
if (found)
425+
{
426+
idx = index;
427+
break;
428+
}
429+
}
430+
394431
if (idx >= 0)
395432
return $"{{{idx}}}";
396433

@@ -455,7 +492,7 @@ private static Expression UnwrapConverted(Expression expr)
455492
if (expr is SqlFunctionExpression func)
456493
{
457494
if (string.Equals(func.Name, "COALESCE", StringComparison.InvariantCultureIgnoreCase) &&
458-
func.Arguments.Count == 2 && func.Arguments[1].NodeType == ExpressionType.Default)
495+
func.Arguments.Count == 2 && func.Arguments[1].NodeType == ExpressionType.Extension)
459496
return UnwrapConverted(func.Arguments[0]);
460497
}
461498

Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFTools.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ static bool InitializeInternal()
7777

7878
LinqExtensions.ExtensionsAdapter = new LinqToDBExtensionsAdapter();
7979

80-
// Set linq2db to allow multiple queries by default
81-
Common.Configuration.Linq.AllowMultipleQuery = true;
82-
8380
return true;
8481
}
8582

0 commit comments

Comments
 (0)