修改基础仓储

This commit is contained in:
不做码农 2021-10-17 17:34:57 +08:00
parent ac51e7a01a
commit fe05697687
2 changed files with 52 additions and 28 deletions

View File

@ -16,17 +16,37 @@ namespace ZR.Repository
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class BaseRepository<T> : SimpleClient<T>, IBaseRepository<T> where T : class, new()
public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
{
public BaseRepository(ISqlSugarClient dbContext = null, int configId = 0) : base(dbContext)
public ISqlSugarClient Context;
//private ISqlSugarClient _db
//{
// get
// {
// if (typeof(T).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription))
// {
// Context.ChangeDatabase(sugarTable.TableDescription.ToLower());
// }
// else
// {
// Context.ChangeDatabase(0);
// }
// return Context;
// }
//}
public BaseRepository(ISqlSugarClient dbContext = null, string configId = "0") //: base(dbContext)
{
if (dbContext == null)
{
//Console.WriteLine("configId" + configId);
base.Context = DbScoped.SugarScope.GetConnection(configId);//根据类传入的ConfigId自动选择
Context = DbScoped.SugarScope.GetConnection(configId);//根据类传入的ConfigId自动选择
//_dbbase = Context;
}
}
//public ISqlSugarClient Db
//{
// get { return _db; }
//}
#region add
/// <summary>
/// 插入指定列使用
@ -72,7 +92,11 @@ namespace ZR.Repository
public int Insert(List<T> t)
{
return base.Context.Insertable(t).ExecuteCommand();
return Context.Insertable(t).ExecuteCommand();
}
public long InsertReturnBigIdentity(T t)
{
return Context.Insertable(t).ExecuteReturnBigIdentity();
}
//public int InsertIgnoreNullColumn(List<T> t)
@ -178,7 +202,7 @@ namespace ZR.Repository
};
}
//base.Context.Updateable(entity).IgnoreColumns(c => list.Contains(c)).Where(isNull).ExecuteCommand()
return base.Context.Updateable(entity).IgnoreColumns(isNull).IgnoreColumns(list.ToArray()).ExecuteCommand() > 0;
return Context.Updateable(entity).IgnoreColumns(isNull).IgnoreColumns(list.ToArray()).ExecuteCommand() > 0;
}
//public bool Update(List<T> entity)
@ -191,7 +215,7 @@ namespace ZR.Repository
//}
public bool Update(Expression<Func<T, bool>> where, Expression<Func<T, T>> columns)
{
return base.Context.Updateable<T>().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommand() > 0;
return Context.Updateable<T>().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommand() > 0;
}
#endregion update
@ -215,10 +239,10 @@ namespace ZR.Repository
#region delete
//public bool DeleteExp(Expression<Func<T, bool>> expression)
//{
// return Context.Deleteable<T>().Where(expression).ExecuteCommand() > 0;
//}
public int Delete(Expression<Func<T, bool>> expression)
{
return Context.Deleteable<T>().Where(expression).ExecuteCommand();
}
//public bool Delete<PkType>(PkType[] primaryKeyValues)
//{
@ -257,7 +281,7 @@ namespace ZR.Repository
// return Context.Queryable(tableName, shortName);
//}
public List<T> QueryableToList(Expression<Func<T, bool>> expression)
public List<T> GetList(Expression<Func<T, bool>> expression)
{
return Context.Queryable<T>().Where(expression).ToList();
}
@ -278,11 +302,6 @@ namespace ZR.Repository
// return Context.Queryable<T>(tableName).ToList();
//}
//public List<T> QueryableToList(string tableName, Expression<Func<T, bool>> expression)
//{
// return Context.Queryable<T>(tableName).Where(expression).ToList();
//}
//public (List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, int pageIndex = 0, int pageSize = 10)
//{
// int totalNumber = 0;
@ -313,10 +332,10 @@ namespace ZR.Repository
// }
//}
public List<T> SqlQueryToList(string sql, object obj = null)
{
return Context.Ado.SqlQuery<T>(sql, obj);
}
//public List<T> SqlQueryToList(string sql, object obj = null)
//{
// return Context.Ado.SqlQuery<T>(sql, obj);
//}
/// <summary>
/// 获得一条数据
/// </summary>
@ -363,6 +382,11 @@ namespace ZR.Repository
{
return Context.Queryable<T>().WithCacheIF(useCache, cacheSecond).ToList();
}
public int Count(Expression<Func<T, bool>> where)
{
return Context.Queryable<T>().Count(where);
}
#endregion query
/// <summary>

View File

@ -36,7 +36,7 @@ namespace ZR.Repository
//DbResult<bool> InsertTran(T t);
//DbResult<bool> InsertTran(List<T> t);
long InsertReturnBigIdentity(T t);
//T InsertReturnEntity(T t);
//T InsertReturnEntity(T t, string sqlWith = SqlWith.UpdLock);
@ -89,7 +89,7 @@ namespace ZR.Repository
#region delete
//bool DeleteExp(Expression<Func<T, bool>> expression);
int Delete(Expression<Func<T, bool>> expression);
//bool Delete<PkType>(PkType[] primaryKeyValues);
//int Delete(object[] obj);
@ -118,7 +118,7 @@ namespace ZR.Repository
//ISugarQueryable<T, T1, T2> Queryable<T1, T2>() where T1 : class where T2 : new();
List<T> QueryableToList(Expression<Func<T, bool>> expression);
List<T> GetList(Expression<Func<T, bool>> expression);
//Task<List<T>> QueryableToListAsync(Expression<Func<T, bool>> expression);
@ -126,8 +126,6 @@ namespace ZR.Repository
//List<T> QueryableToList(string tableName);
//List<T> QueryableToList(string tableName, Expression<Func<T, bool>> expression);
//(List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, int pageIndex = 0, int pageSize = 10);
//(List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, string order, int pageIndex = 0, int pageSize = 10);
@ -136,7 +134,7 @@ namespace ZR.Repository
//(List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, Bootstrap.BootstrapParams bootstrap);
List<T> SqlQueryToList(string sql, object obj = null);
//List<T> SqlQueryToList(string sql, object obj = null);
/// <summary>
/// 获得一条数据
/// </summary>
@ -152,6 +150,8 @@ namespace ZR.Repository
/// <returns></returns>
//T GetFirst(string parm);
int Count(Expression<Func<T, bool>> where);
#endregion query
#region Procedure