mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-25 10:08:11 +08:00
Add Insertable.CallEntityMethod
Add Updateable.CallEntityMethod
This commit is contained in:
parent
6bc53c1239
commit
6b4e0b4f20
@ -80,6 +80,7 @@
|
|||||||
<Compile Include="UnitTest\Test01.cs" />
|
<Compile Include="UnitTest\Test01.cs" />
|
||||||
<Compile Include="UnitTest\UEnum.cs" />
|
<Compile Include="UnitTest\UEnum.cs" />
|
||||||
<Compile Include="UnitTest\UFilter.cs" />
|
<Compile Include="UnitTest\UFilter.cs" />
|
||||||
|
<Compile Include="UnitTest\UInsert2.cs" />
|
||||||
<Compile Include="UnitTest\UInsert.cs" />
|
<Compile Include="UnitTest\UInsert.cs" />
|
||||||
<Compile Include="UnitTest\UQueryable2.cs" />
|
<Compile Include="UnitTest\UQueryable2.cs" />
|
||||||
<Compile Include="UnitTest\UQueue.cs" />
|
<Compile Include="UnitTest\UQueue.cs" />
|
||||||
|
@ -33,6 +33,7 @@ namespace OrmTest
|
|||||||
{
|
{
|
||||||
Filter();
|
Filter();
|
||||||
Insert();
|
Insert();
|
||||||
|
Insert2();
|
||||||
Enum();
|
Enum();
|
||||||
Tran();
|
Tran();
|
||||||
Queue();
|
Queue();
|
||||||
|
43
Src/Asp.Net/SqlServerTest/UnitTest/UInsert2.cs
Normal file
43
Src/Asp.Net/SqlServerTest/UnitTest/UInsert2.cs
Normal file
@ -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<UnitInsertMethod>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -667,6 +667,20 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IInsertable<T> CallEntityMethod(Expression<Action<T>> 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<DbColumnInfo>();
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -173,6 +173,20 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region Update by object
|
#region Update by object
|
||||||
|
public IUpdateable<T> CallEntityMethod(Expression<Action<T>> 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<DbColumnInfo>();
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public IUpdateable<T> WhereColumns(Expression<Func<T, object>> columns)
|
public IUpdateable<T> WhereColumns(Expression<Func<T, object>> columns)
|
||||||
{
|
{
|
||||||
|
@ -87,6 +87,7 @@ namespace SqlSugar
|
|||||||
IUpdateable<T> EnableDiffLogEvent(object businessData = null);
|
IUpdateable<T> EnableDiffLogEvent(object businessData = null);
|
||||||
IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression);
|
IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression);
|
||||||
IUpdateable<T> RemoveDataCache();
|
IUpdateable<T> RemoveDataCache();
|
||||||
|
IUpdateable<T> CallEntityMethod(Expression<Action<T>> method);
|
||||||
KeyValuePair<string,List<SugarParameter>> ToSql();
|
KeyValuePair<string,List<SugarParameter>> ToSql();
|
||||||
void AddQueue();
|
void AddQueue();
|
||||||
|
|
||||||
|
@ -32,6 +32,8 @@ namespace SqlSugar
|
|||||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> subForeignKey);
|
ISubInsertable<T> AddSubList(Expression<Func<T, object>> subForeignKey);
|
||||||
ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
|
ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
|
||||||
|
|
||||||
|
IInsertable<T> CallEntityMethod(Expression<Action<T>> method);
|
||||||
|
|
||||||
IInsertable<T> EnableDiffLogEvent(object businessData = null);
|
IInsertable<T> EnableDiffLogEvent(object businessData = null);
|
||||||
IInsertable<T> RemoveDataCache();
|
IInsertable<T> RemoveDataCache();
|
||||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||||
|
@ -6,6 +6,7 @@ using System.Data.SqlClient;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
@ -320,5 +321,26 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void DataInoveByExpresson<Type>(Type[] datas, MethodCallExpression callExpresion)
|
||||||
|
{
|
||||||
|
var methodInfo = callExpresion.Method;
|
||||||
|
foreach (var item in datas)
|
||||||
|
{
|
||||||
|
if (callExpresion.Arguments.Count == 0)
|
||||||
|
{
|
||||||
|
methodInfo.Invoke(item, null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<object> methodParameters = new List<object>();
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user