修复内嵌dbcontext的替换bug

This commit is contained in:
xuejiaming 2022-06-08 22:14:18 +08:00
parent 807dd4fbb1
commit 154feb72b6
4 changed files with 32 additions and 5 deletions

View File

@ -1,9 +1,9 @@
:start :start
::定义版本 ::定义版本
set EFCORE2=2.5.0.3 set EFCORE2=2.5.0.4
set EFCORE3=3.5.0.3 set EFCORE3=3.5.0.4
set EFCORE5=5.5.0.3 set EFCORE5=5.5.0.4
set EFCORE6=6.5.0.3 set EFCORE6=6.5.0.4
::删除所有bin与obj下的文件 ::删除所有bin与obj下的文件
@echo off @echo off

View File

@ -393,6 +393,7 @@ namespace Sample.SqlServer.Controllers
public async Task<IActionResult> Get7() public async Task<IActionResult> Get7()
{ {
var resultx112331tt2 = await _defaultTableDbContext.Set<SysTest>().Where(o => o.Id == "2") var resultx112331tt2 = await _defaultTableDbContext.Set<SysTest>().Where(o => o.Id == "2")
.Select(o => new .Select(o => new
{ {

View File

@ -165,7 +165,7 @@ namespace ShardingCore.Extensions
{ {
if (memberExpression == null) if (memberExpression == null)
throw new ArgumentNullException(nameof(memberExpression)); 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) public static Type GetSequenceType(this Type type)

View File

@ -48,6 +48,11 @@ namespace ShardingCore.Core.Internal.Visitors
{ {
return ReplaceMemberExpression(queryable); return ReplaceMemberExpression(queryable);
} }
if (value is DbContext dbContext)
{
return ReplaceMemberExpression(dbContext);
}
//return Expression.Constant(value); //return Expression.Constant(value);
} }
@ -58,6 +63,10 @@ namespace ShardingCore.Core.Internal.Visitors
{ {
return ReplaceMemberExpression(queryable); return ReplaceMemberExpression(queryable);
} }
if (value is DbContext dbContext)
{
return ReplaceMemberExpression(dbContext);
}
} }
} }
} }
@ -76,6 +85,14 @@ namespace ShardingCore.Core.Internal.Visitors
Expression.Property(ConstantExpression.Constant(tempVariable), nameof(TempVariable<object>.Queryable)); Expression.Property(ConstantExpression.Constant(tempVariable), nameof(TempVariable<object>.Queryable));
return queryableMemberReplaceExpression; 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> internal sealed class TempVariable<T1>
{ {
@ -86,6 +103,15 @@ namespace ShardingCore.Core.Internal.Visitors
Queryable = queryable; Queryable = queryable;
} }
} }
internal sealed class TempDbVariable<T1>
{
public T1 DbContext { get; }
public TempDbVariable(T1 dbContext)
{
DbContext = dbContext;
}
}
} }