Update queryable.ToSql()

This commit is contained in:
sunkaixuna 2021-08-03 21:08:07 +08:00
parent 2babcd9be7
commit e7ed715874
3 changed files with 24 additions and 7 deletions

View File

@ -22,7 +22,6 @@ namespace OrmTest
public void Test() public void Test()
{ {
base.db.BeginTran(); base.db.BeginTran();
base.GetList(); //调用内部仓储方法 base.GetList(); //调用内部仓储方法
base.ChangeRepository<Repository<C2Table>>().GetList();//调用外部仓储 base.ChangeRepository<Repository<C2Table>>().GetList();//调用外部仓储
@ -57,7 +56,8 @@ namespace OrmTest
var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId; var configId = typeof(T).GetCustomAttribute<TenantAttribute>().configId;
Context = db.GetConnection(configId); Context = db.GetConnection(configId);
this.db = db; this.db = db;
this.db.CodeFirst.InitTables<T>();
} }
} }

View File

@ -1177,11 +1177,16 @@ namespace SqlSugar
public virtual KeyValuePair<string, List<SugarParameter>> ToSql() public virtual KeyValuePair<string, List<SugarParameter>> ToSql()
{ {
InitMapping(); if (!QueryBuilder.IsClone)
ToSqlBefore(); {
string sql = QueryBuilder.ToSqlString(); var newQueryable = this.Clone();
RestoreMapping(); newQueryable.QueryBuilder.IsClone = true;
return new KeyValuePair<string, List<SugarParameter>>(sql, QueryBuilder.Parameters); return newQueryable.ToSql();
}
else
{
return _ToSql();
}
} }
public ISugarQueryable<T> WithCache(int cacheDurationInSeconds = int.MaxValue) public ISugarQueryable<T> WithCache(int cacheDurationInSeconds = int.MaxValue)
{ {
@ -1464,6 +1469,14 @@ namespace SqlSugar
#endregion #endregion
#region Private Methods #region Private Methods
public virtual KeyValuePair<string, List<SugarParameter>> _ToSql()
{
InitMapping();
ToSqlBefore();
string sql = QueryBuilder.ToSqlString();
RestoreMapping();
return new KeyValuePair<string, List<SugarParameter>>(sql, QueryBuilder.Parameters);
}
private List<T> GetChildList(Expression<Func<T, object>> parentIdExpression, string pkName, List<T> list, object rootValue,bool isRoot=true) private List<T> GetChildList(Expression<Func<T, object>> parentIdExpression, string pkName, List<T> list, object rootValue,bool isRoot=true)
{ {
var exp = (parentIdExpression as LambdaExpression).Body; var exp = (parentIdExpression as LambdaExpression).Body;

View File

@ -646,6 +646,10 @@ namespace SqlSugar
#endregion #endregion
#region NoCopy
internal bool IsClone { get; set; }
#endregion
private string GetTableName(string entityName) private string GetTableName(string entityName)
{ {
if (this.AsTables != null && this.AsTables.Any(it=>it.Key==entityName)) if (this.AsTables != null && this.AsTables.Any(it=>it.Key==entityName))