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