From 40bdcc3e2cf801266544d4f97e08c31d7b9f876a Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 2 Apr 2023 18:04:35 +0800 Subject: [PATCH] Update OptLock --- .../Abstract/UpdateProvider/UpdateableHelper.cs | 15 +++++++++++++-- .../Abstract/UpdateProvider/UpdateableProvider.cs | 8 ++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs index 5c715238f..8215b12c7 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs @@ -478,7 +478,17 @@ namespace SqlSugar parameter.DbType = System.Data.DbType.Int16; } } - + private void OptRollBack(int updateRows,T updateData, object oldValue, string name) + { + if (updateRows == 0) + { + var verInfo = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName == name); + if (verInfo != null) + { + verInfo.PropertyInfo.SetValue(updateData, oldValue); + } + } + } private string GetDbColumnName(string propertyName) { if (!IsMappingColumns) @@ -601,7 +611,7 @@ namespace SqlSugar this.RemoveCacheFunc(); } } - private string _ExecuteCommandWithOptLock(T updateData) + private string _ExecuteCommandWithOptLock(T updateData,ref object oldVerValue) { Check.ExceptionEasy(UpdateParameterIsNull == true, "Optimistic lock can only be an entity update method", "乐观锁只能是实体更新方式"); var verColumn = this.EntityInfo.Columns.FirstOrDefault(it => it.IsEnableUpdateVersionValidation); @@ -609,6 +619,7 @@ namespace SqlSugar Check.ExceptionEasy(UpdateObjs.Length > 1, $"Optimistic lock can only handle a single update ", $"乐观锁只能处理单条更新"); Check.ExceptionEasy(!verColumn.UnderType.IsIn(UtilConstants.StringType, UtilConstants.LongType, UtilConstants.GuidType, UtilConstants.DateType), $"Optimistic locks can only be guid, long, and string types", $"乐观锁只能是Guid、Long和字符串类型"); var oldValue = verColumn.PropertyInfo.GetValue(updateData); + oldVerValue = oldValue; var newValue = UtilMethods.GetRandomByType(verColumn.UnderType); verColumn.PropertyInfo.SetValue(updateData, newValue); var data = this.UpdateBuilder.DbColumnInfoList.FirstOrDefault(it => diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index a1ec60a17..9cd2c7c3b 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -70,8 +70,10 @@ namespace SqlSugar Check.ExceptionEasy(this.UpdateBuilder.IsListUpdate==true, " OptLock can only be used on a single object, and the argument cannot be List", "乐观锁只能用于单个对象,参数不能是List,如果是一对多操作请更新主表统一用主表验证"); var updateData = UpdateObjs.FirstOrDefault(); if (updateData == null) return 0; - var name=_ExecuteCommandWithOptLock(updateData); + object oldValue = null; + var name=_ExecuteCommandWithOptLock(updateData,ref oldValue); var result= thi