Skip to content

Commit

Permalink
修复内嵌dbcontext的替换bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xuejmnet committed Jun 8, 2022
1 parent 807dd4f commit 154feb7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
8 changes: 4 additions & 4 deletions nuget-publish.bat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
:start
::定义版本
set EFCORE2=2.5.0.3
set EFCORE3=3.5.0.3
set EFCORE5=5.5.0.3
set EFCORE6=6.5.0.3
set EFCORE2=2.5.0.4
set EFCORE3=3.5.0.4
set EFCORE5=5.5.0.4
set EFCORE6=6.5.0.4

::删除所有bin与obj下的文件
@echo off
Expand Down
1 change: 1 addition & 0 deletions samples/Sample.SqlServer/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ public async Task<IActionResult> Get6()
public async Task<IActionResult> Get7()
{


var resultx112331tt2 = await _defaultTableDbContext.Set<SysTest>().Where(o => o.Id == "2")
.Select(o => new
{
Expand Down
2 changes: 1 addition & 1 deletion src/ShardingCore/Extensions/CommonExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static bool IsMemberQueryable(this MemberExpression memberExpression)
{
if (memberExpression == null)
throw new ArgumentNullException(nameof(memberExpression));
return memberExpression.Type.FullName?.StartsWith("System.Linq.IQueryable`1") ?? false;
return (memberExpression.Type.FullName?.StartsWith("System.Linq.IQueryable`1") ?? false) || typeof(DbContext).IsAssignableFrom(memberExpression.Type);
}

public static Type GetSequenceType(this Type type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ protected override Expression VisitMember
{
return ReplaceMemberExpression(queryable);
}

if (value is DbContext dbContext)
{
return ReplaceMemberExpression(dbContext);
}
//return Expression.Constant(value);
}

Expand All @@ -58,6 +63,10 @@ protected override Expression VisitMember
{
return ReplaceMemberExpression(queryable);
}
if (value is DbContext dbContext)
{
return ReplaceMemberExpression(dbContext);
}
}
}
}
Expand All @@ -76,6 +85,14 @@ private MemberExpression ReplaceMemberExpression(IQueryable queryable)
Expression.Property(ConstantExpression.Constant(tempVariable), nameof(TempVariable<object>.Queryable));
return queryableMemberReplaceExpression;
}
private MemberExpression ReplaceMemberExpression(DbContext dbContext)
{
var tempVariableGenericType = typeof(TempDbVariable<>).GetGenericType0(dbContext.GetType());
var tempVariable = Activator.CreateInstance(tempVariableGenericType, _dbContext);
MemberExpression queryableMemberReplaceExpression =
Expression.Property(ConstantExpression.Constant(tempVariable), nameof(TempDbVariable<object>.DbContext));
return queryableMemberReplaceExpression;
}

internal sealed class TempVariable<T1>
{
Expand All @@ -86,6 +103,15 @@ public TempVariable(IQueryable<T1> queryable)
Queryable = queryable;
}
}
internal sealed class TempDbVariable<T1>
{
public T1 DbContext { get; }

public TempDbVariable(T1 dbContext)
{
DbContext = dbContext;
}
}

}

Expand Down

0 comments on commit 154feb7

Please sign in to comment.