Browse Source

Add Insertable.CallEntityMethod

Add Updateable.CallEntityMethod
pull/2/head
SUNKAIXUAN 4 years ago
parent
commit
6b4e0b4f20
  1. 1
      Src/Asp.Net/SqlServerTest/SqlServerTest.csproj
  2. 1
      Src/Asp.Net/SqlServerTest/UnitTest/Main.cs
  3. 43
      Src/Asp.Net/SqlServerTest/UnitTest/UInsert2.cs
  4. 14
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs
  5. 14
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs
  6. 1
      Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs
  7. 2
      Src/Asp.Net/SqlSugar/Interface/Insertable.cs
  8. 22
      Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs

1
Src/Asp.Net/SqlServerTest/SqlServerTest.csproj

@ -80,6 +80,7 @@
<Compile Include="UnitTest\Test01.cs" />
<Compile Include="UnitTest\UEnum.cs" />
<Compile Include="UnitTest\UFilter.cs" />
<Compile Include="UnitTest\UInsert2.cs" />
<Compile Include="UnitTest\UInsert.cs" />
<Compile Include="UnitTest\UQueryable2.cs" />
<Compile Include="UnitTest\UQueue.cs" />

1
Src/Asp.Net/SqlServerTest/UnitTest/Main.cs

@ -33,6 +33,7 @@ namespace OrmTest
{
Filter();
Insert();
Insert2();
Enum();
Tran();
Queue();

43
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<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;
}
}
}
}

14
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

@ -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
}

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

@ -173,6 +173,20 @@ namespace SqlSugar
}
#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)
{

1
Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs

@ -87,6 +87,7 @@ namespace SqlSugar
IUpdateable<T> EnableDiffLogEvent(object businessData = null);
IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression);
IUpdateable<T> RemoveDataCache();
IUpdateable<T> CallEntityMethod(Expression<Action<T>> method);
KeyValuePair<string,List<SugarParameter>> ToSql();
void AddQueue();

2
Src/Asp.Net/SqlSugar/Interface/Insertable.cs

@ -32,6 +32,8 @@ namespace SqlSugar
ISubInsertable<T> AddSubList(Expression<Func<T, object>> subForeignKey);
ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
IInsertable<T> CallEntityMethod(Expression<Action<T>> method);
IInsertable<T> EnableDiffLogEvent(object businessData = null);
IInsertable<T> RemoveDataCache();
KeyValuePair<string, List<SugarParameter>> ToSql();

22
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>(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…
Cancel
Save