From 27c0531119a7fec7b87d231ef97a69eaecc31459 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Thu, 18 Dec 2025 21:30:56 +0800 Subject: [PATCH] =?UTF-8?q?=20-=20=E4=BF=AE=E5=A4=8D=20preview20251217=20b?= =?UTF-8?q?ug=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/Program.cs | 115 ++++++++++++++++++++++++- FreeSql/FreeSql.xml | 123 --------------------------- FreeSql/Internal/CommonExpression.cs | 2 +- 3 files changed, 112 insertions(+), 128 deletions(-) diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index 563790889..b75827073 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -564,13 +564,13 @@ namespace base_entity #region 初始化 IFreeSql var fsql = new FreeSql.FreeSqlBuilder() - //.UseAutoSyncStructure(true) + .UseAutoSyncStructure(true) .UseNoneCommandParameter(true) //.UseNameConvert(NameConvertType.ToLower) .UseMappingPriority(MappingPriorityType.Attribute, MappingPriorityType.FluentApi, MappingPriorityType.Aop) - .UseAdoConnectionPool(true) + //.UseAdoConnectionPool(true) - .UseConnectionString(FreeSql.DataType.Sqlite, "data source=123.db") + .UseConnectionString(FreeSql.DataType.Sqlite, "data source=:memory:") //.UseConnectionString(DataType.Sqlite, "data source=db1.db;attachs=db2.db") //.UseSlave("data source=test1.db", "data source=test2.db", "data source=test3.db", "data source=test4.db") //.UseSlaveWeight(10, 1, 1, 5) @@ -579,7 +579,7 @@ namespace base_entity //.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5") //.UseQuoteSqlName(false) - .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true") + //.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true") //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true") //.UseAdoConnectionPool(false) @@ -621,6 +621,63 @@ namespace base_entity BaseEntity.Initialization(fsql, () => _asyncUow.Value); #endregion + Console.WriteLine("--- 正在初始化测试数据 ---"); + + // 插入班级 + var class1Id = (int)fsql.Insert(new ClassesEntity { ClassesName = "高三一班" }).ExecuteIdentity(); + var class2Id = (int)fsql.Insert(new ClassesEntity { ClassesName = "高三二班" }).ExecuteIdentity(); + + // 插入小组信息 + var groupId1 = (int)fsql.Insert(new BizDingGroupEntity { GroupCode = "G001", GroupName = "数学竞赛组" }).ExecuteIdentity(); + var groupId2 = (int)fsql.Insert(new BizDingGroupEntity { GroupCode = "G002", GroupName = "英语角" }).ExecuteIdentity(); + var groupId3 = (int)fsql.Insert(new BizDingGroupEntity { GroupCode = "G003", GroupName = "物理实验组" }).ExecuteIdentity(); + + // 插入学生 + var s1Id = (int)fsql.Insert(new StudentEntity { Name = "张三", ClassId = class1Id, CreatedTime = DateTime.Now }).ExecuteIdentity(); + var s2Id = (int)fsql.Insert(new StudentEntity { Name = "李四", ClassId = class1Id, CreatedTime = DateTime.Now.AddDays(-1) }).ExecuteIdentity(); + var s3Id = (int)fsql.Insert(new StudentEntity { Name = "王五", ClassId = class2Id, CreatedTime = DateTime.Now.AddMinutes(-10) }).ExecuteIdentity(); + + // 插入学生与小组的关联 (BizStudentGroupEntity) + fsql.Insert(new List { + new BizStudentGroupEntity { StudentId = s1Id, GroupId = groupId1 }, // 张三 - 数学 + new BizStudentGroupEntity { StudentId = s1Id, GroupId = groupId2 }, // 张三 - 英语 + new BizStudentGroupEntity { StudentId = s2Id, GroupId = groupId1 }, // 李四 - 数学 + new BizStudentGroupEntity { StudentId = s3Id, GroupId = groupId3 } // 王五 - 物理 + }).ExecuteAffrows(); + + var _studentRepository = fsql.GetRepository(); + var _filter = new FilterDto { SchoolClassGroupCode = "G001" }; + var input = new { CurrentPage = 1, PageSize = 10 }; + + // 2. 模拟图片中的查询逻辑 + var listxx2 = fsql.Select() + .LeftJoin((a, b) => a.ClassId == b.Id) + .OrderByDescending((a, b) => a.CreatedTime) + .Count(out var totalxx2) + .Page(input.CurrentPage, input.PageSize) + .ToList((a, b) => new StudentGetPageOutput + { + ClassName = b.ClassesName, + + SchoolClassGroups = fsql.Select() + .LeftJoin((f, g) => f.GroupId == g.Id && f.StudentId == a.Id) + .WhereIf(!string.IsNullOrEmpty(_filter.SchoolClassGroupCode), + (f, g) => g.GroupCode == _filter.SchoolClassGroupCode) + .ToList((f, g) => new IdNameDto + { + Id = g.Id, + Name = g.GroupName + }) + }); + Console.WriteLine($"\n查询完成,总学生数: {totalxx2}"); + foreach (var item in listxx2) + { + var groups = item.SchoolClassGroups != null + ? string.Join(", ", item.SchoolClassGroups.Select(x => x.Name)) + : "无"; + Console.WriteLine($"班级: {item.ClassName} | 关联小组: {groups}"); + } + var orderStatusValue = Order1.OrderStatus.Cancelled; fsql.Select().Where(a => a.status == Order1.OrderStatus.Completed).InsertInto(null, a => new Order1 { @@ -3735,4 +3792,54 @@ public partial class platactioninuser [JsonProperty] public int? PlatStatus { get; set; } +} + +public class StudentEntity +{ + [Column(IsIdentity = true)] public int Id { get; set; } + public string Name { get; set; } + public int ClassId { get; set; } + public DateTime CreatedTime { get; set; } +} + +public class ClassesEntity +{ + [Column(IsIdentity = true)] public int Id { get; set; } + public string ClassesName { get; set; } +} + +// 关联表:学生与小组 +public class BizStudentGroupEntity +{ + [Column(IsIdentity = true)] public int Id { get; set; } + public int StudentId { get; set; } + public int GroupId { get; set; } +} + +// 小组信息表 +public class BizDingGroupEntity +{ + [Column(IsIdentity = true)] public int Id { get; set; } + public string GroupCode { get; set; } + public string GroupName { get; set; } +} + +// --- DTO 输出类 --- + +public class StudentGetPageOutput +{ + public string ClassName { get; set; } + public List SchoolClassGroups { get; set; } +} + +public class IdNameDto +{ + public int Id { get; set; } + public string Name { get; set; } +} + +// 模拟过滤器 +public class FilterDto +{ + public string SchoolClassGroupCode { get; set; } } \ No newline at end of file diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index c41755e18..1f4f5d651 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -6529,126 +6529,3 @@ - - - - - 插入数据,传入实体集合 - - - - - - - - 插入数据,传入实体集合 - - - - - - - - 插入或更新数据,此功能依赖数据库特性(低版本可能不支持),参考如下: - MySql 5.6+: on duplicate key update - PostgreSQL 9.4+: on conflict do update - SqlServer 2008+: merge into - Oracle 11+: merge into - Sqlite: replace into - DuckDB: on conflict do update - 达梦: merge into - 人大金仓:on conflict do update - 神通:merge into - MsAccess:不支持 - 注意区别:FreeSql.Repository 仓储也有 InsertOrUpdate 方法(不依赖数据库特性) - - - - - - - 修改数据 - - - - - - - 修改数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - - - 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - - - - - 查询数据 - - - - - - - 查询数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - - - 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - - - - - 删除数据 - - - - - - - 删除数据,传入动态条件,如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1} - - - 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 - - - - - 开启事务(不支持异步) - v1.5.0 关闭了线程事务超时自动提交的机制 - - 事务体 () => {} - - - - 开启事务(不支持异步) - v1.5.0 关闭了线程事务超时自动提交的机制 - - - 事务体 () => {} - - - - 数据库访问对象 - - - - - 所有拦截方法都在这里 - - - - - CodeFirst 模式开发相关方法 - - - - - DbFirst 模式开发相关方法 - - - - - 全局过滤设置,可默认附加为 Select/Update/Delete 条件 - - - - diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index 23930530b..6748ca2a3 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -482,7 +482,7 @@ namespace FreeSql.Internal break; } var mapType = initExpArg.Type; - if (dtoTable?.ColumnsByCs.TryGetValue(initExp.Bindings[a].Member.Name, out var dtoCol) != null && + if (dtoTable?.ColumnsByCs.TryGetValue(initExp.Bindings[a].Member.Name, out var dtoCol) == true && dtoCol.Attribute.IsIgnore != true) mapType = dtoCol.Attribute.MapType; var child = new ReadAnonymousTypeInfo