diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 570bb47c5..89c5bbf32 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -80,6 +80,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs index 28814164c..39a00f5c3 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs @@ -33,6 +33,7 @@ namespace OrmTest { Filter(); Insert(); + Insert2(); Enum(); Tran(); Queue(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UInsert2.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert2.cs new file mode 100644 index 000000000..63a95716d --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert2.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + public static void Insert2() + { + var db = Db; + db.CodeFirst.InitTables(); + db.Insertable(new UnitInsertMethod() { Name = "1" }).CallEntityMethod(it=>it.Create()).ExecuteCommand(); + db.Insertable(new UnitInsertMethod() { Name = "2" }).CallEntityMethod(it => it.Create("admin")).ExecuteCommand(); + db.Updateable(new UnitInsertMethod() {Id=1, Name = "1" }).CallEntityMethod(it => it.Create()).ExecuteCommand(); + db.Updateable(new UnitInsertMethod() { Name = "1" }).CallEntityMethod(it => it.Create("admint")).ExecuteCommand(); + } + + public class UnitInsertMethod + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public DateTime Time { get; set; } + [SqlSugar.SugarColumn(IsNullable =true)] + public string UserId { get; set; } + + public void Create() + { + this.Time = DateTime.Now; + this.UserId = "1"; + } + public void Create(string a) + { + this.Time = DateTime.Now; + this.UserId = a; + } + } + + } +} diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 9c2dabb57..1efa61dee 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -667,6 +667,20 @@ namespace SqlSugar } } + + public IInsertable CallEntityMethod(Expression> method) + { + if (this.InsertObjs.HasValue()) + { + var expression = (LambdaExpression.Lambda(method).Body as LambdaExpression).Body; + Check.Exception(!(expression is MethodCallExpression), method.ToString() + " is not method"); + var callExpresion = expression as MethodCallExpression; + UtilMethods.DataInoveByExpresson(this.InsertObjs,callExpresion); + this.InsertBuilder.DbColumnInfoList = new List(); + Init(); + } + return this; + } #endregion } diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index fb5ab8606..6f4013479 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -173,6 +173,20 @@ namespace SqlSugar } #region Update by object + public IUpdateable CallEntityMethod(Expression> method) + { + ThrowUpdateByExpression(); + if (this.UpdateObjs.HasValue()) + { + var expression = (LambdaExpression.Lambda(method).Body as LambdaExpression).Body; + Check.Exception(!(expression is MethodCallExpression), method.ToString() + " is not method"); + var callExpresion = expression as MethodCallExpression; + UtilMethods.DataInoveByExpresson(this.UpdateObjs, callExpresion); + this.UpdateBuilder.DbColumnInfoList = new List(); + Init(); + } + return this; + } public IUpdateable WhereColumns(Expression> columns) { diff --git a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs index 82dd5f3d5..fa2aa7359 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs @@ -87,6 +87,7 @@ namespace SqlSugar IUpdateable EnableDiffLogEvent(object businessData = null); IUpdateable ReSetValue(Expression> setValueExpression); IUpdateable RemoveDataCache(); + IUpdateable CallEntityMethod(Expression> method); KeyValuePair> ToSql(); void AddQueue(); diff --git a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs index 497774703..431c35071 100644 --- a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs @@ -32,6 +32,8 @@ namespace SqlSugar ISubInsertable AddSubList(Expression> subForeignKey); ISubInsertable AddSubList(Expression> tree); + IInsertable CallEntityMethod(Expression> method); + IInsertable EnableDiffLogEvent(object businessData = null); IInsertable RemoveDataCache(); KeyValuePair> ToSql(); diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs index df121e761..30c8a6b19 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs @@ -6,6 +6,7 @@ using System.Data.SqlClient; using System.Diagnostics; using System.Globalization; using System.Linq; +using System.Linq.Expressions; using System.Reflection; using System.Runtime.CompilerServices; using System.Security.Cryptography; @@ -320,5 +321,26 @@ namespace SqlSugar } } + public static void DataInoveByExpresson(Type[] datas, MethodCallExpression callExpresion) + { + var methodInfo = callExpresion.Method; + foreach (var item in datas) + { + if (callExpresion.Arguments.Count == 0) + { + methodInfo.Invoke(item, null); + } + else + { + List methodParameters = new List(); + foreach (var callItem in callExpresion.Arguments) + { + var parameter = callItem.GetType().GetProperties().First(it => it.Name == "Value").GetValue(callItem, null); + methodParameters.Add(parameter); + } + methodInfo.Invoke(item, methodParameters.ToArray()); + } + } + } } }