From db94cd32d5a1fecf442dcd2ab7e64fb26308e1b7 Mon Sep 17 00:00:00 2001 From: Bi8bo <2738644273@qq.com> Date: Mon, 2 Sep 2024 15:01:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8D=95=E5=AE=9E=E4=BD=93=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=94=AF=E6=8C=81abp=20IHasConcurrencyStamp=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3,=E4=B9=90=E8=A7=82=E9=94=81=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/SqlSugarRepository.cs | 6 ++++++ .../Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs index 38cf18d3..537a5051 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs @@ -372,6 +372,12 @@ namespace Yi.Framework.SqlSugarCore.Repositories public virtual async Task UpdateAsync(TEntity updateObj) { + if (typeof(TEntity).IsAssignableTo())//带版本号乐观锁更新 + { + int num = await (await GetDbSimpleClientAsync()) + .Context.Updateable(updateObj).ExecuteCommandWithOptLockAsync(); + return num>0; + } return await (await GetDbSimpleClientAsync()).UpdateAsync(updateObj); } diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs index 528ac602..7600dfb1 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs @@ -320,9 +320,11 @@ namespace Yi.Framework.SqlSugarCore /// protected virtual void EntityService(PropertyInfo property, EntityColumnInfo column) { - if (property.Name == "ConcurrencyStamp") + if (property.Name == nameof(IHasConcurrencyStamp.ConcurrencyStamp)) //带版本号并发更新 { - column.IsIgnore = true; + // column.IsOnlyIgnoreInsert = true; + // column.IsOnlyIgnoreUpdate = true; + column.IsEnableUpdateVersionValidation = true; } if (property.PropertyType == typeof(ExtraPropertyDictionary)) { From eb8d1626eaeaf6b45f0ddf8137420aa7708325e8 Mon Sep 17 00:00:00 2001 From: Bi8bo <2738644273@qq.com> Date: Mon, 2 Sep 2024 16:06:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B9=B6=E5=8F=91=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E4=BF=AE=E6=94=B9=E4=B8=BA=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8A=9B=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/SqlSugarRepository.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs index 537a5051..50bad3c5 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs @@ -1,8 +1,11 @@ using System.Linq; using System.Linq.Expressions; +using System.Text; +using Microsoft.Extensions.Logging; using SqlSugar; using Volo.Abp; using Volo.Abp.Auditing; +using Volo.Abp.Data; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Volo.Abp.Linq; @@ -374,9 +377,17 @@ namespace Yi.Framework.SqlSugarCore.Repositories { if (typeof(TEntity).IsAssignableTo())//带版本号乐观锁更新 { - int num = await (await GetDbSimpleClientAsync()) - .Context.Updateable(updateObj).ExecuteCommandWithOptLockAsync(); - return num>0; + try + { + int num = await (await GetDbSimpleClientAsync()) + .Context.Updateable(updateObj).ExecuteCommandWithOptLockAsync(true); + return num>0; + } + catch (VersionExceptions ex) + { + + throw new AbpDbConcurrencyException($"{ex.Message}[更新失败:ConcurrencyStamp不是最新版本],entityInfo:{updateObj}", ex); + } } return await (await GetDbSimpleClientAsync()).UpdateAsync(updateObj); }