Browse Source

WhereColumns add check

pull/12/MERGE
610262374@qq.com 6 years ago
parent
commit
8a74acfbef
  1. 4
      Src/Asp.Net/SqlServerTest/Demos/2_Update.cs
  2. 9
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs

4
Src/Asp.Net/SqlServerTest/Demos/2_Update.cs

@ -115,6 +115,10 @@ namespace OrmTest.Demo
.Where(p => p.SchoolId == SqlFunc.Subqueryable<Student>().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<Student>() { new Student() { }, new Student() { } }).UpdateColumns(it => new { it.Name, it.CreateTime }).WhereColumns(it => it.CreateTime).ExecuteCommand();
}
}
}

9
Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs

@ -24,6 +24,7 @@ namespace SqlSugar
public List<MappingColumn> MappingColumnList { get; set; }
private List<string> IgnoreColumnNameList { get; set; }
private List<string> 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<T> WhereColumns(Expression<Func<T, object>> columns)
{
this.IsWhereColumns = true;
Check.Exception(UpdateParameterIsNull==true, "Updateable<T>().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<string>();
@ -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())
{

Loading…
Cancel
Save