diff --git a/Src/Asp.Net/SqlServerTest/Demos/2_Update.cs b/Src/Asp.Net/SqlServerTest/Demos/2_Update.cs index a2a3df84b..a7c259ee8 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/2_Update.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/2_Update.cs @@ -115,6 +115,10 @@ namespace OrmTest.Demo .Where(p => p.SchoolId == SqlFunc.Subqueryable().Where(s => s.SchoolId == p.Id).Select(s => s.Id)).ExecuteCommand(); var t24 = db.Updateable(new Student() { }).WhereColumns(it=>it.CreateTime).ExecuteCommand(); + + var t25 = db.Updateable(new Student() { }).UpdateColumns(it=> new { it.Name,it.CreateTime}).WhereColumns(it => it.CreateTime).ExecuteCommand(); + + var t26 = db.Updateable(new List() { new Student() { }, new Student() { } }).UpdateColumns(it => new { it.Name, it.CreateTime }).WhereColumns(it => it.CreateTime).ExecuteCommand(); } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index cd1826449..fac89ba29 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -24,6 +24,7 @@ namespace SqlSugar public List MappingColumnList { get; set; } private List IgnoreColumnNameList { get; set; } private List WhereColumnList { get; set; } + private bool IsWhereColumns { get; set; } private bool IsOffIdentity { get; set; } private bool IsVersionValidation { get; set; } public MappingTableList OldMappingTableList { get; set; } @@ -153,6 +154,7 @@ namespace SqlSugar public IUpdateable WhereColumns(Expression> columns) { + this.IsWhereColumns = true; Check.Exception(UpdateParameterIsNull==true, "Updateable().Updateable is error,Use Updateable(obj).WhereColumns"); var whereColumns = UpdateBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => this.SqlBuilder.GetNoTranslationColumnName(it)).ToList(); if (this.WhereColumnList == null) this.WhereColumnList = new List(); @@ -394,6 +396,13 @@ namespace SqlSugar private void PreToSql() { UpdateBuilder.PrimaryKeys = GetPrimaryKeys(); + if (this.IsWhereColumns) { + foreach (var pkName in UpdateBuilder.PrimaryKeys) + { + var isContains=this.UpdateBuilder.DbColumnInfoList.Select(it => it.DbColumnName.ToLower()).Contains(pkName.ToLower()); + Check.Exception(isContains == false, "Use UpdateColumns().WhereColumn() ,UpdateColumns need {0}", pkName); + } + } #region IgnoreColumns if (this.Context.IgnoreColumns != null && this.Context.IgnoreColumns.Any()) {