From 87c9ca8b9b5d05170562c1b58da72904aea28b0f Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Tue, 11 Mar 2025 16:08:55 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20DbSet/Repository=20Com?= =?UTF-8?q?pareState=20=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=9B#1994?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/Program.cs | 20 ++ FreeSql.DbContext/DbSet/DbSet.cs | 4 +- FreeSql.DbContext/FreeSql.DbContext.xml | 8 - .../CompareStateTest.cs | 45 ++++ FreeSql/FreeSql.xml | 232 ++++++++---------- 5 files changed, 176 insertions(+), 133 deletions(-) create mode 100644 FreeSql.Tests/FreeSql.Tests.DbContext2/CompareStateTest.cs diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index 6e06396d5..a3aa4e058 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -621,6 +621,19 @@ namespace base_entity BaseEntity.Initialization(fsql, () => _asyncUow.Value); #endregion + fsql.InsertOrUpdate().SetSource(new AppInfoEntity { AppID = "03DN8CW8", AppName = "app_01" }).ExecuteAffrows(); + var repo2211 = fsql.GetRepository(); + + var appInfo = repo2211.Where(info => info.AppID == "03DN8CW8").First(); + appInfo = repo2211.Where(info => info.AppID == "03DN8CW8").First(); + var compareDic = new Dictionary(); + var updateInfo = ""; + + repo2211.Attach(appInfo); + appInfo.AppName = "测试"; + compareDic = repo2211.CompareState(appInfo); + Console.WriteLine(appInfo.AppName); + var sql20250205 = fsql.Select() .InnerJoin((l, p) => l.ProductId == p.ID) .GroupBy((l, p) => new { p.ID, ShopType = l.ShopType ?? 0 }) @@ -2997,6 +3010,13 @@ class String_TestIdAndIdentity : TypeHandler } } } + public class AppInfoEntity + { + [Column(IsPrimary = true, Name = "APP_ID")] + public string AppID { get; set; } + [Column(Name = "APP_NAME")] + public string AppName { get; set; } + } public partial class OrderLine22x { diff --git a/FreeSql.DbContext/DbSet/DbSet.cs b/FreeSql.DbContext/DbSet/DbSet.cs index 25614a1ab..eb692d9b4 100644 --- a/FreeSql.DbContext/DbSet/DbSet.cs +++ b/FreeSql.DbContext/DbSet/DbSet.cs @@ -281,8 +281,8 @@ namespace FreeSql return _db.OrmOriginal.CompareEntityValueReturnColumns(_entityType, oldState.Value, newdata, false).ToDictionary(a => a, a => new object[] { - _db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, newdata, a), - _db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, oldState.Value, a) + _db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, newdata, _table.Columns[a].CsName), + _db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, oldState.Value, _table.Columns[a].CsName) }); } diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index c941a15d9..a7a94fcda 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -758,13 +758,5 @@ - - - 批量注入 Repository,可以参考代码自行调整 - - - - - diff --git a/FreeSql.Tests/FreeSql.Tests.DbContext2/CompareStateTest.cs b/FreeSql.Tests/FreeSql.Tests.DbContext2/CompareStateTest.cs new file mode 100644 index 000000000..7894229a8 --- /dev/null +++ b/FreeSql.Tests/FreeSql.Tests.DbContext2/CompareStateTest.cs @@ -0,0 +1,45 @@ +using FreeSql.DataAnnotations; +using FreeSql.Internal; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace FreeSql.Tests.DbContext2 +{ + public class CompareStateTest + { + [Fact] + public void TestCompareState() + { + using (var fsql = g.CreateMemory()) + { + fsql.Insert(new AppInfoEntity { AppID = "03DN8CW8", AppName = "app_01" }).ExecuteAffrows(); + var repo = fsql.GetRepository(); + + var appInfo = repo.Where(info => info.AppID == "03DN8CW8").First(); + appInfo = repo.Where(info => info.AppID == "03DN8CW8").First(); + var compareDic = new Dictionary(); + var updateInfo = ""; + + repo.Attach(appInfo); + appInfo.AppName = "测试"; + compareDic = repo.CompareState(appInfo); + Console.WriteLine(appInfo.AppName); + + } + } + + public class AppInfoEntity + { + [Column(IsPrimary = true, Name = "APP_ID")] + public string AppID { get; set; } + [Column(Name = "APP_NAME")] + public string AppName { get; set; } + } + + } +} diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 0e71315ae..6e6fddabc 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -1087,6 +1087,93 @@ + + + 动态创建实体类型 + + + + + 配置Class + + 类名 + 类标记的特性[Table(Name = "xxx")] [Index(xxxx)] + + + + + 获取类型构建器,可作为要构建的Type来引用 + + + + + 配置属性 + + 属性名称 + 属性类型 + 属性标记的特性-支持多个 + + + + + 配置属性 + + 属性名称 + 属性类型 + 该属性是否重写父类属性 + 属性标记的特性-支持多个 + + + + + 配置属性 + + 属性名称 + 属性类型 + 该属性是否重写父类属性 + 属性默认值 + 属性标记的特性-支持多个 + + + + + 配置父类 + + 父类类型 + + + + + Override属性 + + + + + + Emit动态创建出Class - Type + + + + + + Emit动态创建出Class - Type,不附带获取TableInfo + + + + + + 首字母小写 + + + + + + + 首字母大写 + + + + 获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 "" @@ -5868,6 +5955,28 @@ 对象池 + + + 动态构建Class Type + + + + + + 根据字典,创建 table 对应的实体对象 + + + + + + + + 根据实体对象,创建 table 对应的字典 + + + + + C#: that >= between && that <= and @@ -6434,126 +6543,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 条件 - - - -