From b7eda4c803b35f7829532b5a0775ea271ae3e0ec Mon Sep 17 00:00:00 2001 From: skx <610262374@qq.com> Date: Mon, 4 Jan 2021 14:59:26 +0800 Subject: [PATCH] Multi database query page bug --- .../SqlServerTest/SqlServerTest.csproj | 1 + Src/Asp.Net/SqlServerTest/UnitTest/Test01.cs | 100 ++++++++++++++++++ .../QueryableProvider/QueryableProvider.cs | 6 ++ 3 files changed, 107 insertions(+) create mode 100644 Src/Asp.Net/SqlServerTest/UnitTest/Test01.cs diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index bf12d1263..ec74aa796 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -77,6 +77,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Test01.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Test01.cs new file mode 100644 index 000000000..891385426 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Test01.cs @@ -0,0 +1,100 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + [SugarTable("UnitUser")] + public class User2 + { + [SugarColumn(IsPrimaryKey = true)] + public int Id { set; get; } + public string Name { set; get; } + public int DepartmentId { set; get; } + } + [SugarTable("UnitOrder")] + public class Order2 + { + [SugarColumn(IsPrimaryKey = true)] + public int Id { set; get; } + public string Name { set; get; } + public int UserId { set; get; } + } + [SugarTable("UnitDepartment")] + public class Department2 + { + [SugarColumn(IsPrimaryKey = true)] + public int Id { set; get; } + public string Name { set; get; } + } + public static async Task QueryableAsync2() + { + DataBaseInitialize(); + var context = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = "Data Source=.;Initial Catalog=test2222;User id=sa;Password=haosql;pooling=true;min pool size = 2;max pool size=100;", + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute, + }); + //调式代码 用来打印SQL + context.Aop.OnLogExecuting = (sql, pars) => + { + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "开始\r\n" + sql + "\r\n" + context.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); + }; + context.Aop.OnLogExecuted = (sql, pars) => + { + Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "结束\r\n" + sql + "\r\n" + context.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); + }; + var recordCount = new RefAsync(); + //var result = await context.Queryable((o, u, d) => new JoinQueryInfos + var result = await context.Queryable((o, u) => new JoinQueryInfos + ( + JoinType.Left, o.UserId == u.Id + //,JoinType.Left, u.DepartmentId == d.Id + )) + .AS("dbo.UnitOrder") + .AS("test1111.dbo.UnitUser") + //.AS("test1.dbo.Department") + .Select(o => new User2() { Id = o.Id, Name = o.Name }) + .ToPageListAsync(1, 10, recordCount); + + Console.ReadLine(); + } + public static void DataBaseInitialize() + { + //数据库1 + var context1 = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = "Data Source=.;Initial Catalog=test1111;User id=sa;Password=haosql;pooling=true;min pool size = 2;max pool size=100;", + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute, + }); + context1.DbMaintenance.CreateDatabase(); + context1.CodeFirst.InitTables(typeof(User2)); + context1.CodeFirst.InitTables(typeof(Department2)); + if (context1.Queryable().Where(t => t.Id == 1).Count() == 0) + context1.Insertable(new User2() { Id = 1, Name = "TestUser", DepartmentId = 1 }).ExecuteCommand(); + if (context1.Queryable().Where(t => t.Id == 1).Count() == 0) + context1.Insertable(new Department2() { Id = 1, Name = "TestDepartment" }).ExecuteCommand(); + //数据库2 + var context2 = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = "Data Source=.;Initial Catalog=test2222;User id=sa;Password=haosql;pooling=true;min pool size = 2;max pool size=100;", + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute, + }); + context2.DbMaintenance.CreateDatabase(); + context2.CodeFirst.InitTables(typeof(Order2)); + if (context1.Queryable().Where(t => t.Id == 1).Count() == 0) + context2.Insertable(new Order2() { Id = 1, Name = "Order", UserId = 1 }).ExecuteCommand(); + } + } +} diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index a2692884e..37f090aa1 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1083,7 +1083,9 @@ namespace SqlSugar } public async Task> ToPageListAsync(int pageIndex, int pageSize, RefAsync totalNumber) { + var oldMapping = this.Context.MappingTables; totalNumber.Value = await this.Clone().CountAsync(); + this.Context.MappingTables = oldMapping; return await this.Clone().ToPageListAsync(pageIndex, pageSize); } public async Task ToJsonAsync() @@ -1108,7 +1110,9 @@ namespace SqlSugar } public async Task ToJsonPageAsync(int pageIndex, int pageSize, RefAsync totalNumber) { + var oldMapping = this.Context.MappingTables; totalNumber.Value = await this.Clone().CountAsync(); + this.Context.MappingTables = oldMapping; return await this.Clone().ToJsonPageAsync(pageIndex, pageSize); } public async Task ToDataTableAsync() @@ -1135,7 +1139,9 @@ namespace SqlSugar } public async Task ToDataTablePageAsync(int pageIndex, int pageSize, RefAsync totalNumber) { + var oldMapping = this.Context.MappingTables; totalNumber.Value = await this.Clone().CountAsync(); + this.Context.MappingTables = oldMapping; return await this.Clone().ToDataTablePageAsync(pageIndex, pageSize); }