From b4333c2fe2c758f7f01599cb947eb8b1285d98df Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Sun, 21 Nov 2021 12:10:44 +0800 Subject: [PATCH] Update logic delete bug --- .../SqlSeverTest/Demo/Demo2_Updateable.cs | 1 + .../SqlSeverTest/Demo/Demo4_Deleteable.cs | 11 +++++++++++ .../Abstract/DeleteProvider/LogicDeleteProvider.cs | 4 ++-- .../Abstract/UpdateProvider/UpdateableProvider.cs | 9 ++++++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/Demo2_Updateable.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/Demo2_Updateable.cs index c32884aab..fe911ddb0 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/Demo2_Updateable.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/Demo2_Updateable.cs @@ -100,6 +100,7 @@ namespace OrmTest //Where Sql //db.Updateable(updateObj).Where("id=@x", new { x = "1" }).ExecuteCommand(); + db.Updateable().SetColumns("name", 1).Where(it => it.Id == 1).ExecuteCommand(); Console.WriteLine("#### Updateable End ####"); } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/Demo4_Deleteable.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/Demo4_Deleteable.cs index 8e39e2013..15db55232 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/Demo4_Deleteable.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demo/Demo4_Deleteable.cs @@ -41,8 +41,19 @@ namespace OrmTest //by expression db.Deleteable().Where(it => it.Id == 11111).ExecuteCommand(); + //logic delete + db.CodeFirst.InitTables(); + db.Deleteable().Where(it=>it.Name=="a").IsLogic().ExecuteCommand(); Console.WriteLine("#### Deleteable End ####"); } + + public class LogicDeleteTezt + { + public string Name { get; set; } + [SugarColumn(IsPrimaryKey =true)] + public int Id { get; set; } + public bool IsDeleted { get; set; } + } } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/LogicDeleteProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/LogicDeleteProvider.cs index 103b189e8..9e29ffd2e 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/LogicDeleteProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/DeleteProvider/LogicDeleteProvider.cs @@ -17,11 +17,11 @@ namespace SqlSugar List pars; string where; LogicFieldName = _ExecuteCommand(LogicFieldName, out db, out where, out pars); - var updateable = db.Updateable().SetColumns(LogicFieldName, "@IsDeleted"); + var updateable = db.Updateable().SetColumns(LogicFieldName,true); if (pars != null) updateable.UpdateBuilder.Parameters.AddRange(pars); Convert(updateable as UpdateableProvider); - var result = updateable.Where(where, new { IsDeleted = true }).ExecuteCommand(); + var result = updateable.Where(where).ExecuteCommand(); return result; } public async Task ExecuteCommandAsync(string LogicFieldName = null) diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 7227bbd25..b1e619175 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -299,7 +299,14 @@ namespace SqlSugar { fieldName = columnInfo.DbColumnName; } - UpdateBuilder.SetValues.Add(new KeyValuePair(fieldName, fieldValue+"")); + var parameterName =this.SqlBuilder.SqlParameterKeyWord+ "Const" + this.UpdateBuilder.LambdaExpressions.ParameterIndex; + this.UpdateBuilder.LambdaExpressions.ParameterIndex = this.UpdateBuilder.LambdaExpressions.ParameterIndex+1; + if (UpdateBuilder.Parameters == null) + { + UpdateBuilder.Parameters = new List(); + } + UpdateBuilder.Parameters.Add(new SugarParameter(parameterName, fieldValue)); + UpdateBuilder.SetValues.Add(new KeyValuePair(SqlBuilder.GetTranslationColumnName(fieldName), $"{SqlBuilder.GetTranslationColumnName(fieldName)}={parameterName}")); this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => (UpdateParameterIsNull == false && IsPrimaryKey(it)) || UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList(); AppendSets(); return this;