mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-26 14:15:50 +08:00
Update OptLock
This commit is contained in:
@@ -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 =>
|
||||
|
||||
@@ -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= this.ExecuteCommand();
|
||||
OptRollBack(result,updateData, oldValue, name);
|
||||
if (result == 0 && IsVersionValidation)
|
||||
{
|
||||
throw new VersionExceptions(string.Format("UpdateVersionValidation {0} Not the latest version ", name));
|
||||
@@ -112,8 +114,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= await this.ExecuteCommandAsync();
|
||||
OptRollBack(result,updateData, oldValue, name);
|
||||
if (result == 0 && IsVersionValidation)
|
||||
{
|
||||
throw new VersionExceptions(string.Format("UpdateVersionValidation {0} Not the latest version ", name));
|
||||
|
||||
Reference in New Issue
Block a user