From 2348bbff3e99cb9d3c9605caa0e0be1465d8de4d Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 4 Jun 2017 12:13:21 +0800 Subject: [PATCH] Update IInsertable --- SqlServerTest/Demos/Insert.cs | 3 ++- .../InsertableProvider/InsertableProvider.cs | 17 ++++++++++++----- SqlSugar/Interface/Insertable.cs | 1 + 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/SqlServerTest/Demos/Insert.cs b/SqlServerTest/Demos/Insert.cs index 8c5b5de63..f4b2301fb 100644 --- a/SqlServerTest/Demos/Insert.cs +++ b/SqlServerTest/Demos/Insert.cs @@ -24,8 +24,9 @@ namespace OrmTest.Demo var t3 = db.Insertable(insertObj).ExecuteReutrnIdentity(); - //Only insert Name + //Only insert Name and SchoolId var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.SchoolId }).ExecuteReutrnIdentity(); + var t4_1 = db.Insertable(insertObj).InsertColumns(it => it=="Name"||it== "SchoolId").ExecuteReutrnIdentity(); //Ignore TestId diff --git a/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 82e2fb597..189b92614 100644 --- a/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -51,7 +51,7 @@ namespace SqlSugar PreToSql(); string sql = InsertBuilder.ToSqlString(); RestoreMapping(); - return Ado.GetInt(sql, InsertBuilder.Parameters==null?null:InsertBuilder.Parameters.ToArray()); + return Ado.GetInt(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()); } #endregion @@ -84,6 +84,12 @@ namespace SqlSugar return this; } + public IInsertable InsertColumns(Func insertColumMethod) + { + this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it => insertColumMethod(it.PropertyName)).ToList(); + return this; + } + public IInsertable With(string lockString) { this.InsertBuilder.TableWithString = lockString; @@ -132,8 +138,9 @@ namespace SqlSugar foreach (var item in this.InsertBuilder.DbColumnInfoList) { if (this.InsertBuilder.Parameters == null) this.InsertBuilder.Parameters = new List(); - var paramters = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value,item.PropertyType); - if (InsertBuilder.IsInsertNull && paramters.Value == null) { + var paramters = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType); + if (InsertBuilder.IsInsertNull && paramters.Value == null) + { continue; } this.InsertBuilder.Parameters.Add(paramters); @@ -152,10 +159,10 @@ namespace SqlSugar { var columnInfo = new DbColumnInfo() { - Value = column.PropertyInfo.GetValue(item,null), + Value = column.PropertyInfo.GetValue(item, null), DbColumnName = GetDbColumnName(column.PropertyName), PropertyName = column.PropertyName, - PropertyType=PubMethod.GetUnderType(column.PropertyInfo), + PropertyType = PubMethod.GetUnderType(column.PropertyInfo), TableId = i }; insertItem.Add(columnInfo); diff --git a/SqlSugar/Interface/Insertable.cs b/SqlSugar/Interface/Insertable.cs index ddd70aec1..21307429d 100644 --- a/SqlSugar/Interface/Insertable.cs +++ b/SqlSugar/Interface/Insertable.cs @@ -14,6 +14,7 @@ namespace SqlSugar IInsertable AS(string tableName); IInsertable With(string lockString); IInsertable InsertColumns(Expression> columns); + IInsertable InsertColumns(Func insertColumMethod); IInsertable IgnoreColumns(Expression> columns); IInsertable IgnoreColumns(Func ignoreColumMethod); IInsertable Where(bool isInsertNull, bool isOffIdentity = false);