diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 537315e2..26522f10 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -800,5 +800,14 @@ + + + 批量注入 Repository,可以参考代码自行调整 + + + + + + diff --git a/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml b/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml index a0d7a412..4ee3e866 100644 --- a/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml +++ b/FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml @@ -149,6 +149,11 @@ 用户 + + + 用户 + + 收款金额 diff --git a/FreeSql.Tests/FreeSql.Tests/Issues/1208.cs b/FreeSql.Tests/FreeSql.Tests/Issues/1208.cs index 22f81bce..5358365d 100644 --- a/FreeSql.Tests/FreeSql.Tests/Issues/1208.cs +++ b/FreeSql.Tests/FreeSql.Tests/Issues/1208.cs @@ -8,7 +8,7 @@ namespace FreeSql.Tests.Issues public class _1208 { [Fact] - public void GlobalFilter() + public void GlobalFilter01() { using (var fsql = new FreeSqlBuilder() .UseConnectionString(DataType.Sqlite, "data source=:memory:") @@ -171,6 +171,174 @@ ORDER BY a.""Id"" DESC", sql); public ICollection Users { get; set; } public int TenantId { get; set; } } - } + [Fact] + public void GlobalFilter02() + { + using (var fsql = new FreeSqlBuilder() + .UseConnectionString(DataType.Sqlite, "data source=:memory:") + .UseAutoSyncStructure(true) + .Build()) + { + fsql.GlobalFilter.Apply("TenantQuery", a => a.TenantId == 100); + fsql.GlobalFilter.Apply("SoftDeleted", a => a.SoftDeleted == false); + + var userRepository = fsql.GetRepository(); + + var deviceId = 100; + var sql = userRepository.Select + .Where(i => i.DoorDevices.AsSelect().Any(x => x.Id == deviceId)) + .OrderByDescending(true, a => a.Id) + .ToSql(); + Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted"" +FROM ""issues1208_User02"" a +WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDeviceUser02"" Mx_Mi + WHERE (Mx_Mi.""UserId"" = a.""Id"") AND (exists(SELECT 1 + FROM ""issues1208_DoorDevice02"" x + WHERE (x.""Id"" = 100) AND (x.""Id"" = Mx_Mi.""DoorDeviceId"") AND (x.""TenantId"" = 100) AND (x.""SoftDeleted"" = 0) + limit 0,1)) AND (Mx_Mi.""TenantId"" = 100) AND (Mx_Mi.""SoftDeleted"" = 0) + limit 0,1)) AND (a.""TenantId"" = 100) AND (a.""SoftDeleted"" = 0) +ORDER BY a.""Id"" DESC", sql); + + sql = userRepository.Select + .Where(i => i.DoorDevices.AsSelect().DisableGlobalFilter("TenantQuery").Any(x => x.Id == deviceId)) + .OrderByDescending(true, a => a.Id) + .ToSql(); + Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted"" +FROM ""issues1208_User02"" a +WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDeviceUser02"" Mx_Mi + WHERE (Mx_Mi.""UserId"" = a.""Id"") AND (exists(SELECT 1 + FROM ""issues1208_DoorDevice02"" x + WHERE (x.""Id"" = 100) AND (x.""Id"" = Mx_Mi.""DoorDeviceId"") AND (x.""SoftDeleted"" = 0) + limit 0,1)) AND (Mx_Mi.""SoftDeleted"" = 0) + limit 0,1)) AND (a.""TenantId"" = 100) AND (a.""SoftDeleted"" = 0) +ORDER BY a.""Id"" DESC", sql); + + sql = userRepository.Select.DisableGlobalFilter("TenantQuery") + .Where(i => i.DoorDevices.AsSelect().Any(x => x.Id == deviceId)) + .OrderByDescending(true, a => a.Id) + .ToSql(); + Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted"" +FROM ""issues1208_User02"" a +WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDeviceUser02"" Mx_Mi + WHERE (Mx_Mi.""UserId"" = a.""Id"") AND (exists(SELECT 1 + FROM ""issues1208_DoorDevice02"" x + WHERE (x.""Id"" = 100) AND (x.""Id"" = Mx_Mi.""DoorDeviceId"") AND (x.""SoftDeleted"" = 0) + limit 0,1)) AND (Mx_Mi.""SoftDeleted"" = 0) + limit 0,1)) AND (a.""SoftDeleted"" = 0) +ORDER BY a.""Id"" DESC", sql); + + using (userRepository.DataFilter.Disable("TenantQuery")) + { + sql = userRepository.Select + .Where(i => i.DoorDevices.AsSelect().Any(x => x.Id == deviceId)) + .OrderByDescending(true, a => a.Id) + .ToSql(); + Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted"" +FROM ""issues1208_User02"" a +WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDeviceUser02"" Mx_Mi + WHERE (Mx_Mi.""UserId"" = a.""Id"") AND (exists(SELECT 1 + FROM ""issues1208_DoorDevice02"" x + WHERE (x.""Id"" = 100) AND (x.""Id"" = Mx_Mi.""DoorDeviceId"") AND (x.""SoftDeleted"" = 0) + limit 0,1)) AND (Mx_Mi.""SoftDeleted"" = 0) + limit 0,1)) AND (a.""SoftDeleted"" = 0) +ORDER BY a.""Id"" DESC", sql); + } + + sql = userRepository.Select + .Where(i => i.DoorDevices.Any(x => x.Id == deviceId)) + .OrderByDescending(true, a => a.Id) + .ToSql(); + Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted"" +FROM ""issues1208_User02"" a +WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDevice02"" x + WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDeviceUser02"" Mx_Ma + WHERE (Mx_Ma.""DoorDeviceId"" = x.""Id"") AND (Mx_Ma.""UserId"" = a.""Id"") AND (Mx_Ma.""TenantId"" = 100) AND (Mx_Ma.""SoftDeleted"" = 0) + limit 0,1)) AND (x.""Id"" = 100) AND (x.""TenantId"" = 100) AND (x.""SoftDeleted"" = 0) + limit 0,1)) AND (a.""TenantId"" = 100) AND (a.""SoftDeleted"" = 0) +ORDER BY a.""Id"" DESC", sql); + + sql = userRepository.Select.DisableGlobalFilter("TenantQuery") + .Where(i => i.DoorDevices.Any(x => x.Id == deviceId)) + .OrderByDescending(true, a => a.Id) + .ToSql(); + Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted"" +FROM ""issues1208_User02"" a +WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDevice02"" x + WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDeviceUser02"" Mx_Ma + WHERE (Mx_Ma.""DoorDeviceId"" = x.""Id"") AND (Mx_Ma.""UserId"" = a.""Id"") AND (Mx_Ma.""SoftDeleted"" = 0) + limit 0,1)) AND (x.""Id"" = 100) AND (x.""SoftDeleted"" = 0) + limit 0,1)) AND (a.""SoftDeleted"" = 0) +ORDER BY a.""Id"" DESC", sql); + + using (userRepository.DataFilter.Disable("TenantQuery")) + { + sql = userRepository.Select + .Where(i => i.DoorDevices.Any(x => x.Id == deviceId)) + .OrderByDescending(true, a => a.Id) + .ToSql(); + Assert.Equal(@"SELECT a.""Id"", a.""TenantId"", a.""SoftDeleted"" +FROM ""issues1208_User02"" a +WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDevice02"" x + WHERE (exists(SELECT 1 + FROM ""issues1208_DoorDeviceUser02"" Mx_Ma + WHERE (Mx_Ma.""DoorDeviceId"" = x.""Id"") AND (Mx_Ma.""UserId"" = a.""Id"") AND (Mx_Ma.""SoftDeleted"" = 0) + limit 0,1)) AND (x.""Id"" = 100) AND (x.""SoftDeleted"" = 0) + limit 0,1)) AND (a.""SoftDeleted"" = 0) +ORDER BY a.""Id"" DESC", sql); + } + } + } + + [Table(Name = "issues1208_User02")] + class UserEntity02 + { + [Column(IsIdentity = true)] + public int Id { get; set; } + + [Navigate(ManyToMany = typeof(DoorDeviceUserEntity02))] + public ICollection DoorDevices { get; set; } + + public int TenantId { get; set; } + public bool SoftDeleted { get; set; } + } + [Table(Name = "issues1208_DoorDeviceUser02")] + class DoorDeviceUserEntity02 + { + public int UserId { get; set; } + + [Navigate(nameof(UserId))] + public UserEntity02 User { get; set; } + + public int DoorDeviceId { get; set; } + + [Navigate(nameof(DoorDeviceId))] + public DoorDeviceEntity02 DoorDevice { get; set; } + public int TenantId { get; set; } + public bool SoftDeleted { get; set; } + } + [Table(Name = "issues1208_DoorDevice02")] + class DoorDeviceEntity02 + { + [Column(IsIdentity = true)] + public int Id { get; set; } + + /// + /// 用户 + /// + [Navigate(ManyToMany = typeof(DoorDeviceUserEntity02))] + public ICollection Users { get; set; } + public int TenantId { get; set; } + public bool SoftDeleted { get; set; } + } + } } diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index 9c0bf50d..645876d0 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -1241,14 +1241,8 @@ namespace FreeSql.Internal } if (tsc.whereGlobalFilter != null) { - if (tsc.whereGlobalFilter.Any() == false) - fsqlSelect0._whereGlobalFilter.Clear(); - else - { - var fsqlGlobalFilter = fsqlSelect0._whereGlobalFilter; - if (fsqlGlobalFilter != tsc.whereGlobalFilter) - fsqlGlobalFilter.AddRange(tsc.whereGlobalFilter.Where(b => !fsqlGlobalFilter.Any(a => a.Name == b.Name))); - } + fsqlSelect0._whereGlobalFilter.Clear(); + if (tsc.whereGlobalFilter.Any()) fsqlSelect0._whereGlobalFilter.AddRange(tsc.whereGlobalFilter); } } else if (fsqlType != null) @@ -2375,14 +2369,8 @@ namespace FreeSql.Internal if (tableRule != null) midSelect._tableRules.Add(tableRule); if (whereGlobalFilter != null) { - if (whereGlobalFilter.Any() == false) - midSelect._whereGlobalFilter.Clear(); - else - { - var fsqlGlobalFilter = midSelect._whereGlobalFilter; - if (fsqlGlobalFilter != whereGlobalFilter) - fsqlGlobalFilter.AddRange(whereGlobalFilter.Where(b => !fsqlGlobalFilter.Any(a => a.Name == b.Name))); - } + midSelect._whereGlobalFilter.Clear(); + if (whereGlobalFilter.Any()) midSelect._whereGlobalFilter.AddRange(whereGlobalFilter); } switch (commonExp._ado.DataType) { @@ -2443,14 +2431,8 @@ namespace FreeSql.Internal if (tableRule != null) select._tableRules.Add(tableRule); if (whereGlobalFilter != null) { - if (whereGlobalFilter.Any() == false) - select._whereGlobalFilter.Clear(); - else - { - var fsqlGlobalFilter = select._whereGlobalFilter; - if (fsqlGlobalFilter != whereGlobalFilter) - fsqlGlobalFilter.AddRange(whereGlobalFilter.Where(b => !fsqlGlobalFilter.Any(a => a.Name == b.Name))); - } + select._whereGlobalFilter.Clear(); + if (whereGlobalFilter.Any()) select._whereGlobalFilter.AddRange(whereGlobalFilter); } } while (true)