This commit is contained in:
xuejiaming 2022-11-16 21:41:40 +08:00
parent 3f9624f221
commit 833a234058
3 changed files with 31 additions and 17 deletions

View File

@ -171,19 +171,20 @@ namespace Sample.MySql.Controllers
[HttpGet]
public async Task<IActionResult> Get2()
{
var sql= from a in _defaultTableDbContext.Set<DynamicTable>()
join b in _defaultTableDbContext.Set<SysTest>()
on a.Id equals b.Id into t1
from aa1 in t1.DefaultIfEmpty()
// join bc in _defaultTableDbContext.Set<SysTest>()
// on a.Id equals bc.Id into t2
// from aa2 in t2.DefaultIfEmpty()
select new
{
ID = a.Id
};
var listAsync =await sql.ToListAsync();
// var sysUserMods = await _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync();
// var sql= from a in _defaultTableDbContext.Set<DynamicTable>()
// join b in _defaultTableDbContext.Set<SysTest>()
// on a.Id equals b.Id into t1
// from aa1 in t1.DefaultIfEmpty()
// // join bc in _defaultTableDbContext.Set<SysTest>()
// // on a.Id equals bc.Id into t2
// // from aa2 in t2.DefaultIfEmpty()
// select new
// {
// ID = a.Id
// };
// var listAsync =await sql.ToListAsync();
var sysUserMods1 = await _defaultTableDbContext.Set<SysUserMod>().FromSqlRaw("select * from SysUserMod where id='2'").ToListAsync();
var sysUserMods2 = await _defaultTableDbContext.Set<SysTest>().FromSqlRaw("select * from SysTest where id='2'").ToListAsync();
return Ok();
}
}

View File

@ -15,10 +15,12 @@ namespace ShardingCore.Sharding.ShardingExecutors
{
private readonly IQueryCompiler _queryCompiler;
private readonly Expression _queryExpression;
private readonly Expression _originalQueryExpression;
public QueryCompilerExecutor(DbContext dbContext,Expression queryExpression)
{
_queryCompiler = dbContext.GetService<IQueryCompiler>();
_originalQueryExpression = queryExpression;
_queryExpression = queryExpression.ReplaceDbContextExpression(dbContext);
}

View File

@ -6,6 +6,7 @@ using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.Internal;
using ShardingCore.Core.Internal.Visitors;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
@ -198,11 +199,21 @@ namespace ShardingCore.Core.Internal.Visitors
var newQueryable = targetIQ.Provider.CreateQuery(targetIQ.Expression);
if (Source == null)
Source = newQueryable;
//如何替换ef5的set
var replaceQueryRoot = new ReplaceSingleQueryRootExpressionVisitor();
replaceQueryRoot.Visit(newQueryable.Expression);
RootIsVisit = true;
return base.VisitExtension(replaceQueryRoot.QueryRootExpression);
if (queryRootExpression is FromSqlQueryRootExpression fromSqlQueryRootExpression)
{
var sqlQueryRootExpression = new FromSqlQueryRootExpression(newQueryable.Provider as IAsyncQueryProvider,
queryRootExpression.EntityType, fromSqlQueryRootExpression.Sql,
fromSqlQueryRootExpression.Argument);
return base.VisitExtension(sqlQueryRootExpression);
}
else
{
var replaceQueryRoot = new ReplaceSingleQueryRootExpressionVisitor();
replaceQueryRoot.Visit(newQueryable.Expression);
return base.VisitExtension(replaceQueryRoot.QueryRootExpression);
}
}
return base.VisitExtension(node);