133 lines
5.7 KiB
C#
133 lines
5.7 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq.Expressions;
|
|||
|
|
|
|||
|
|
namespace Mtxfw.Utility.DataAccess
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 修改类
|
|||
|
|
/// </summary>
|
|||
|
|
public partial class Sql : DbHelperSqlSugar
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 根据实体更新(主键要有值,主键是更新条件)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">实体类名</typeparam>
|
|||
|
|
/// <param name="entity">实体类</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int Update<T>(T entity) where T : class, new()
|
|||
|
|
{
|
|||
|
|
return Db.Updateable<T>(entity).ExecuteCommand();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 批量修改
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">实体类名</typeparam>
|
|||
|
|
/// <param name="entitys">实体类集合</param>
|
|||
|
|
/// <param name="predicate">修改条件 -> t => t.id == 1 && t.name == "aaaa"</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int Update<T>(List<T> entitys, Expression<Func<T, bool>> predicate) where T : class, new()
|
|||
|
|
{
|
|||
|
|
return Db.Updateable<T>(entitys.ToArray()).Where(predicate).ExecuteCommand();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 根据表达示中的列更新,指定列并赋值的更新,比较常用
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">实体类名</typeparam>
|
|||
|
|
/// <param name="columns">表达式列 -> it => new Student() { Name = "a", CreateTime = DateTime.Now }</param>
|
|||
|
|
/// <param name="predicate">修改条件 -> it => it.Id == 11</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int Update<T>(Expression<Func<T, T>> columns, Expression<Func<T, bool>> predicate) where T : class, new()
|
|||
|
|
{
|
|||
|
|
return Db.Updateable<T>().UpdateColumns(columns).Where(predicate).ExecuteCommand();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 4.2.3添加了WhereColumns 虽然XId不是主键但是 XId作为更新条件
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">实体名</typeparam>
|
|||
|
|
/// <param name="entity">实体类</param>
|
|||
|
|
/// <param name="predicate">指定字段 -> it=>new{it.XId}</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int UpdateWhereColumn<T>(T entity, Expression<Func<T, object>> predicate) where T : class, new()
|
|||
|
|
{
|
|||
|
|
return Db.Updateable<T>(entity).WhereColumns(predicate).ExecuteCommand();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 更新 指定字段 以外的所有列 (主键要有值,主键是更新条件)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">实体名</typeparam>
|
|||
|
|
/// <param name="entity">实体类</param>
|
|||
|
|
/// <param name="predicate">
|
|||
|
|
/// 指定字段 -> it => new { it.Name, it.TestId }
|
|||
|
|
/// 或者 it => it=="name" 更新NAME 以外的所有列
|
|||
|
|
/// </param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int UpdateIgnoreColumns<T>(T entity, Expression<Func<T, object>> predicate) where T : class, new()
|
|||
|
|
{
|
|||
|
|
return Db.Updateable<T>(entity).IgnoreColumns(predicate).ExecuteCommand();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 实体更新,并且给指定列重新赋值 ,其它列也插入
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">实体名</typeparam>
|
|||
|
|
/// <param name="entity">实体类</param>
|
|||
|
|
/// <param name="predicate">it => it.Name == (it.Name + 1)</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int UpdateReSetValue<T>(T entity, Expression<Func<T, bool>> predicate) where T : class, new()
|
|||
|
|
{
|
|||
|
|
return Db.Updateable<T>(entity).ReSetValue(predicate).ExecuteCommand();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 批量更新,并且给指定列重新赋值 ,其它列也插入
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">实体名</typeparam>
|
|||
|
|
/// <param name="entity">实体类</param>
|
|||
|
|
/// <param name="predicate">it => it.Name == (it.Name + 1)</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int UpdateReSetValue<T>(List<T> list, Expression<Func<T, bool>> predicate) where T : class, new()
|
|||
|
|
{
|
|||
|
|
return Db.Updateable(list).ReSetValue(predicate).ExecuteCommand();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 只更新指定字段并且 (entity不为空的情况)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">实体名</typeparam>
|
|||
|
|
/// <param name="entity">实体类</param>
|
|||
|
|
/// <param name="columns">修改字段 -> it=>new {it.Name}</param>
|
|||
|
|
/// <param name="predicate">修改的值 -> it => it.Name == (it.Name + 1)</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int UpdateColumnsReSetValue<T>(T entity, Expression<Func<T, object>> columns, Expression<Func<T, bool>> predicate) where T : class, new()
|
|||
|
|
{
|
|||
|
|
return Db.Updateable<T>(entity).UpdateColumns(columns).ReSetValue(predicate).ExecuteCommand();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 只更新指定字段并且 (entity为空的情况)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">实体名</typeparam>
|
|||
|
|
/// <param name="columns">修改字段 -> it => new Student() { Name = it.Name+1}</param>
|
|||
|
|
/// <param name="predicate">条件 -> it => it.Id == 11</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public int UpdateColumnsReSetValueToWhere<T>(Expression<Func<T, T>> columns, Expression<Func<T, bool>> predicate) where T : class, new()
|
|||
|
|
{
|
|||
|
|
#pragma warning disable 618
|
|||
|
|
return Db.Updateable<T>().UpdateColumns(columns).Where(predicate).ExecuteCommand();
|
|||
|
|
#pragma warning restore 618
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 修改一列数据
|
|||
|
|
/// </summary>
|
|||
|
|
public bool UpdateField<T>(Dictionary<string, object> dic) where T : class, new()
|
|||
|
|
{
|
|||
|
|
return Db.Updateable<T>(dic).ExecuteCommand() > 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|