diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Insert.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Insert.cs index 27a5027ba..ecd4ccb8a 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Insert.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Insert.cs @@ -124,6 +124,23 @@ new List() { (@Name) ;SELECT SCOPE_IDENTITY();", new List() { new SugarParameter("@Name","张三") }, t11.Key, t11.Value, "Insert t11 error"); + + + var t12 = db.Insertable(new { Name = "a" }).ToSql(); + base.Check(@"INSERT INTO [STudent] + ([Name]) + VALUES + (@Name) ;SELECT SCOPE_IDENTITY();", new List() { + new SugarParameter("@Name","a") + }, t12.Key, t12.Value, "Insert t12 error"); + + var t13 = db.Insertable(new Dictionary() { {"id",0 },{ "name","2"} }).ToSql(); + base.Check(@"INSERT INTO [STudent] + ([Name]) + VALUES + (@Name) ;SELECT SCOPE_IDENTITY();", new List() { + new SugarParameter("@Name","2") + }, t13.Key, t13.Value, "Insert t13 error"); } } diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Update.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Update.cs index f0c5c64e2..bde5ffe24 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Update.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Update.cs @@ -144,6 +144,21 @@ namespace OrmTest.UnitTest "Update 12 error" ); + + + var t13 = db.Updateable(new { Name = "a", id=1 }).ToSql(); + base.Check(@"UPDATE [STudent] SET + [Name]=@Name WHERE [Id]=@Id", new List() { + new SugarParameter("@Name","a"), + new SugarParameter("@ID",1) + }, t13.Key, t13.Value, "Insert t13 error"); + + var t14 = db.Updateable(new Dictionary() { { "id", 0 }, { "name", "2" } }).ToSql(); + base.Check(@"UPDATE [STudent] SET + [Name]=@Name WHERE [Id]=@Id", new List() { + new SugarParameter("@Name", "2"), + new SugarParameter("@ID", 0) + }, t14.Key, t14.Value, "Insert t14 error"); } } diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index 172422d09..87f139e4a 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -265,7 +265,8 @@ namespace SqlSugar { Check.Exception(columnDictionary == null || columnDictionary.Count == 0, "Insertable.columnDictionary can't be null"); var insertObject = this.RewritableMethods.DeserializeObject(this.RewritableMethods.SerializeObject(columnDictionary)); - return this.Insertable(insertObject); + var columns = columnDictionary.Select(it => it.Key).ToList(); + return this.Insertable(insertObject).InsertColumns(it => columns.Any(c => it.Equals(c,StringComparison.CurrentCultureIgnoreCase))); ; } public virtual IInsertable Insertable(dynamic insertDynamicObject) where T : class, new() { @@ -275,8 +276,9 @@ namespace SqlSugar } else { - var insertObject = this.RewritableMethods.DeserializeObject(this.RewritableMethods.SerializeObject(insertDynamicObject)); - return this.Insertable(insertObject); + var columns= ((object)insertDynamicObject).GetType().GetProperties().Select(it => it.Name).ToList(); + T insertObject = this.RewritableMethods.DeserializeObject(this.RewritableMethods.SerializeObject(insertDynamicObject)); + return this.Insertable(insertObject).InsertColumns(it=> columns.Any(c=>it.Equals(c,StringComparison.CurrentCultureIgnoreCase))); } } #endregion @@ -338,7 +340,8 @@ namespace SqlSugar { Check.Exception(columnDictionary == null || columnDictionary.Count == 0, "Updateable.columnDictionary can't be null"); var updateObject = this.RewritableMethods.DeserializeObject(this.RewritableMethods.SerializeObject(columnDictionary)); - return this.Updateable(updateObject); + var columns = columnDictionary.Select(it => it.Key).ToList(); + return this.Updateable(updateObject).UpdateColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase))); ; } public virtual IUpdateable Updateable(dynamic updateDynamicObject) where T : class, new() { @@ -348,8 +351,9 @@ namespace SqlSugar } else { - var updateObject = this.RewritableMethods.DeserializeObject(this.RewritableMethods.SerializeObject(updateDynamicObject)); - return this.Updateable(updateObject); + var columns = ((object)updateDynamicObject).GetType().GetProperties().Select(it => it.Name).ToList(); + T updateObject = this.RewritableMethods.DeserializeObject(this.RewritableMethods.SerializeObject(updateDynamicObject)); + return this.Updateable(updateObject).UpdateColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase))); ; } } #endregion