using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace DbCommon.BusinessCore.DbCore { public class DbContextBase where T : class, new() { public DbContextBase() { } //注意:不能写成静态的 public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作 public SimpleClient CurrentDb { get { return new SimpleClient(Db); } }//用来操作当前表的数据 /// /// 获取所有 /// /// public virtual List GetList() { return CurrentDb.GetList(); } /// /// 根据表达式查询 /// /// public virtual List GetList(Expression> whereExpression) { return CurrentDb.GetList(whereExpression); } /// /// 根据表达式查询分页 /// /// public virtual List GetPageList(Expression> whereExpression, PageModel pageModel) { return CurrentDb.GetPageList(whereExpression, pageModel); } /// /// 根据表达式查询分页并排序 /// /// it /// /// it=>it.id或者it=>new{it.id,it.name} /// OrderByType.Desc /// public virtual List GetPageList(Expression> whereExpression, PageModel pageModel, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) { return CurrentDb.GetPageList(whereExpression, pageModel, orderByExpression, orderByType); } /// /// 根据主键查询 /// /// public virtual T GetById(dynamic id) { return CurrentDb.GetById(id); } /// /// 根据主键删除 /// /// /// public virtual bool Delete(dynamic id) { return CurrentDb.DeleteById(id); } /// /// 根据实体删除 /// /// /// public virtual bool Delete(T data) { return CurrentDb.Delete(data); } /// /// 根据主键删除 /// /// /// public virtual bool Delete(dynamic[] ids) { return CurrentDb.AsDeleteable().In(ids).ExecuteCommand() > 0; } /// /// 根据表达式删除 /// /// /// public virtual bool Delete(Expression> whereExpression) { return CurrentDb.Delete(whereExpression); } /// /// 根据实体更新,实体需要有主键 /// /// /// public virtual bool Update(T obj) { return CurrentDb.Update(obj); } /// ///批量更新 /// /// /// public virtual bool Update(List objs) { return CurrentDb.UpdateRange(objs); } /// /// 插入 /// /// /// public virtual bool Insert(T obj) { return CurrentDb.Insert(obj); } /// /// 批量 /// /// /// public virtual bool Insert(List objs) { return CurrentDb.InsertRange(objs); } //自已扩展更多方法 } }