数据库连接替换成IOC配置
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Infrastructure;
|
||||
using Infrastructure.Model;
|
||||
using SqlSugar;
|
||||
using SqlSugar.IOC;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
@@ -21,46 +22,8 @@ namespace ZR.Repository
|
||||
{
|
||||
if (dbContext == null)
|
||||
{
|
||||
string connStr = ConfigUtils.Instance.GetConnectionStrings(OptionsSetting.ConnAdmin);
|
||||
string dbKey = ConfigUtils.Instance.GetAppConfig<string>(OptionsSetting.DbKey);
|
||||
int dbType = ConfigUtils.Instance.GetAppConfig<int>(OptionsSetting.ConnDbType);
|
||||
if (!string.IsNullOrEmpty(dbKey))
|
||||
{
|
||||
connStr = NETCore.Encrypt.EncryptProvider.DESDecrypt(connStr, dbKey);
|
||||
}
|
||||
|
||||
var Db = new SqlSugarClient(new List<ConnectionConfig>()
|
||||
{
|
||||
new ConnectionConfig(){
|
||||
ConnectionString = connStr,
|
||||
DbType = (DbType)dbType,
|
||||
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
|
||||
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
|
||||
ConfigId = 0
|
||||
},
|
||||
new ConnectionConfig(){
|
||||
ConnectionString = "",
|
||||
DbType = DbType.SqlServer,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
|
||||
ConfigId = 1
|
||||
},
|
||||
});
|
||||
|
||||
//调式代码 用来打印SQL
|
||||
Db.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine("【SQL语句】" + sql.ToLower() + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
||||
};
|
||||
//出错打印日志
|
||||
Db.GetConnection(0).Aop.OnError = (e) =>
|
||||
{
|
||||
Console.WriteLine($"[执行Sql出错]{e.Message},SQL={e.Sql}");
|
||||
Console.WriteLine();
|
||||
};
|
||||
|
||||
Context = Db.GetConnection(configId);//根据类传入的ConfigId自动选择
|
||||
//Console.WriteLine("configId:" + configId);
|
||||
base.Context = DbScoped.SugarScope.GetConnection(configId);//根据类传入的ConfigId自动选择
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,115 +50,115 @@ namespace ZR.Repository
|
||||
return Context.Insertable(t).ExecuteCommand();
|
||||
}
|
||||
|
||||
public int InsertIgnoreNullColumn(T t)
|
||||
{
|
||||
return Context.Insertable(t).IgnoreColumns(true).ExecuteCommand();
|
||||
}
|
||||
//public int InsertIgnoreNullColumn(T t)
|
||||
//{
|
||||
// return Context.Insertable(t).IgnoreColumns(true).ExecuteCommand();
|
||||
//}
|
||||
|
||||
public int InsertIgnoreNullColumn(T t, params string[] columns)
|
||||
{
|
||||
return Context.Insertable(t).IgnoreColumns(columns).ExecuteCommand();
|
||||
}
|
||||
//public int InsertIgnoreNullColumn(T t, params string[] columns)
|
||||
//{
|
||||
// return Context.Insertable(t).IgnoreColumns(columns).ExecuteCommand();
|
||||
//}
|
||||
|
||||
//public int Insert(SqlSugarClient client, T t)
|
||||
//{
|
||||
// return client.Insertable(t).ExecuteCommand();
|
||||
//}
|
||||
|
||||
public long InsertBigIdentity(T t)
|
||||
{
|
||||
return base.Context.Insertable(t).ExecuteReturnBigIdentity();
|
||||
}
|
||||
//public long InsertBigIdentity(T t)
|
||||
//{
|
||||
// return base.Context.Insertable(t).ExecuteReturnBigIdentity();
|
||||
//}
|
||||
|
||||
public int Insert(List<T> t)
|
||||
{
|
||||
return base.Context.Insertable(t).ExecuteCommand();
|
||||
}
|
||||
|
||||
public int InsertIgnoreNullColumn(List<T> t)
|
||||
{
|
||||
return base.Context.Insertable(t).IgnoreColumns(true).ExecuteCommand();
|
||||
}
|
||||
//public int InsertIgnoreNullColumn(List<T> t)
|
||||
//{
|
||||
// return base.Context.Insertable(t).IgnoreColumns(true).ExecuteCommand();
|
||||
//}
|
||||
|
||||
public int InsertIgnoreNullColumn(List<T> t, params string[] columns)
|
||||
{
|
||||
return base.Context.Insertable(t).IgnoreColumns(columns).ExecuteCommand();
|
||||
}
|
||||
public int Insert(T parm, Expression<Func<T, object>> iClumns = null, bool ignoreNull = true)
|
||||
{
|
||||
return base.Context.Insertable(parm).InsertColumns(iClumns).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand();
|
||||
}
|
||||
public DbResult<bool> InsertTran(T t)
|
||||
{
|
||||
var result = base.Context.Ado.UseTran(() =>
|
||||
{
|
||||
base.Context.Insertable(t).ExecuteCommand();
|
||||
});
|
||||
return result;
|
||||
}
|
||||
//public int InsertIgnoreNullColumn(List<T> t, params string[] columns)
|
||||
//{
|
||||
// return base.Context.Insertable(t).IgnoreColumns(columns).ExecuteCommand();
|
||||
//}
|
||||
//public int Insert(T parm, Expression<Func<T, object>> iClumns = null, bool ignoreNull = true)
|
||||
//{
|
||||
// return base.Context.Insertable(parm).InsertColumns(iClumns).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand();
|
||||
//}
|
||||
//public DbResult<bool> InsertTran(T t)
|
||||
//{
|
||||
// var result = base.Context.Ado.UseTran(() =>
|
||||
// {
|
||||
// base.Context.Insertable(t).ExecuteCommand();
|
||||
// });
|
||||
// return result;
|
||||
//}
|
||||
|
||||
public DbResult<bool> InsertTran(List<T> t)
|
||||
{
|
||||
var result = base.Context.Ado.UseTran(() =>
|
||||
{
|
||||
base.Context.Insertable(t).ExecuteCommand();
|
||||
});
|
||||
return result;
|
||||
}
|
||||
//public DbResult<bool> InsertTran(List<T> t)
|
||||
//{
|
||||
// var result = base.Context.Ado.UseTran(() =>
|
||||
// {
|
||||
// base.Context.Insertable(t).ExecuteCommand();
|
||||
// });
|
||||
// return result;
|
||||
//}
|
||||
|
||||
public T InsertReturnEntity(T t)
|
||||
{
|
||||
return base.Context.Insertable(t).ExecuteReturnEntity();
|
||||
}
|
||||
//public T InsertReturnEntity(T t)
|
||||
//{
|
||||
// return base.Context.Insertable(t).ExecuteReturnEntity();
|
||||
//}
|
||||
|
||||
public T InsertReturnEntity(T t, string sqlWith = SqlWith.UpdLock)
|
||||
{
|
||||
return base.Context.Insertable(t).With(sqlWith).ExecuteReturnEntity();
|
||||
}
|
||||
//public T InsertReturnEntity(T t, string sqlWith = SqlWith.UpdLock)
|
||||
//{
|
||||
// return base.Context.Insertable(t).With(sqlWith).ExecuteReturnEntity();
|
||||
//}
|
||||
|
||||
public bool ExecuteCommand(string sql, object parameters)
|
||||
{
|
||||
return base.Context.Ado.ExecuteCommand(sql, parameters) > 0;
|
||||
}
|
||||
//public bool ExecuteCommand(string sql, object parameters)
|
||||
//{
|
||||
// return base.Context.Ado.ExecuteCommand(sql, parameters) > 0;
|
||||
//}
|
||||
|
||||
public bool ExecuteCommand(string sql, params SugarParameter[] parameters)
|
||||
{
|
||||
return base.Context.Ado.ExecuteCommand(sql, parameters) > 0;
|
||||
}
|
||||
//public bool ExecuteCommand(string sql, params SugarParameter[] parameters)
|
||||
//{
|
||||
// return base.Context.Ado.ExecuteCommand(sql, parameters) > 0;
|
||||
//}
|
||||
|
||||
public bool ExecuteCommand(string sql, List<SugarParameter> parameters)
|
||||
{
|
||||
return base.Context.Ado.ExecuteCommand(sql, parameters) > 0;
|
||||
}
|
||||
//public bool ExecuteCommand(string sql, List<SugarParameter> parameters)
|
||||
//{
|
||||
// return base.Context.Ado.ExecuteCommand(sql, parameters) > 0;
|
||||
//}
|
||||
|
||||
#endregion add
|
||||
|
||||
#region update
|
||||
|
||||
public bool UpdateEntity(T entity, bool ignoreNullColumns = false)
|
||||
{
|
||||
return base.Context.Updateable(entity).IgnoreColumns(ignoreNullColumns).ExecuteCommand() > 0;
|
||||
}
|
||||
//public bool UpdateEntity(T entity, bool ignoreNullColumns = false)
|
||||
//{
|
||||
// return base.Context.Updateable(entity).IgnoreColumns(ignoreNullColumns).ExecuteCommand() > 0;
|
||||
//}
|
||||
|
||||
public bool Update(T entity, Expression<Func<T, bool>> expression)
|
||||
{
|
||||
return base.Context.Updateable(entity).Where(expression).ExecuteCommand() > 0;
|
||||
}
|
||||
//public bool Update(T entity, Expression<Func<T, bool>> expression)
|
||||
//{
|
||||
// return base.Context.Updateable(entity).Where(expression).ExecuteCommand() > 0;
|
||||
//}
|
||||
|
||||
public bool Update(T entity, Expression<Func<T, object>> expression, bool ignoreAllNull = false)
|
||||
{
|
||||
return base.Context.Updateable(entity).UpdateColumns(expression).IgnoreColumns(ignoreAllNull).ExecuteCommand() > 0;
|
||||
}
|
||||
//public bool Update(T entity, Expression<Func<T, object>> expression, bool ignoreAllNull = false)
|
||||
//{
|
||||
// return base.Context.Updateable(entity).UpdateColumns(expression).IgnoreColumns(ignoreAllNull).ExecuteCommand() > 0;
|
||||
//}
|
||||
|
||||
public bool Update(T entity, Expression<Func<T, object>> expression, Expression<Func<T, bool>> where)
|
||||
{
|
||||
return base.Context.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand() > 0;
|
||||
}
|
||||
//public bool Update(T entity, Expression<Func<T, object>> expression, Expression<Func<T, bool>> where)
|
||||
//{
|
||||
// return base.Context.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand() > 0;
|
||||
//}
|
||||
|
||||
public bool Update(SqlSugarClient client, T entity, Expression<Func<T, object>> expression, Expression<Func<T, bool>> where)
|
||||
{
|
||||
return client.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand() > 0;
|
||||
}
|
||||
//public bool Update(SqlSugarClient client, T entity, Expression<Func<T, object>> expression, Expression<Func<T, bool>> where)
|
||||
//{
|
||||
// return client.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand() > 0;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
@@ -218,14 +181,14 @@ namespace ZR.Repository
|
||||
return base.Context.Updateable(entity).IgnoreColumns(isNull).IgnoreColumns(list.ToArray()).ExecuteCommand() > 0;
|
||||
}
|
||||
|
||||
public bool Update(List<T> entity)
|
||||
{
|
||||
var result = base.Context.Ado.UseTran(() =>
|
||||
{
|
||||
base.Context.Updateable(entity).ExecuteCommand();
|
||||
});
|
||||
return result.IsSuccess;
|
||||
}
|
||||
//public bool Update(List<T> entity)
|
||||
//{
|
||||
// var result = base.Context.Ado.UseTran(() =>
|
||||
// {
|
||||
// base.Context.Updateable(entity).ExecuteCommand();
|
||||
// });
|
||||
// return result.IsSuccess;
|
||||
//}
|
||||
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;
|
||||
@@ -252,20 +215,20 @@ namespace ZR.Repository
|
||||
|
||||
#region delete
|
||||
|
||||
public bool DeleteExp(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
return Context.Deleteable<T>().Where(expression).ExecuteCommand() > 0;
|
||||
}
|
||||
//public bool DeleteExp(Expression<Func<T, bool>> expression)
|
||||
//{
|
||||
// return Context.Deleteable<T>().Where(expression).ExecuteCommand() > 0;
|
||||
//}
|
||||
|
||||
//public bool Delete<PkType>(PkType[] primaryKeyValues)
|
||||
//{
|
||||
// return base.Context.Deleteable<T>().In(primaryKeyValues).ExecuteCommand() > 0;
|
||||
//}
|
||||
|
||||
public int Delete(object[] obj)
|
||||
{
|
||||
return Context.Deleteable<T>().In(obj).ExecuteCommand();
|
||||
}
|
||||
//public int Delete(object[] obj)
|
||||
//{
|
||||
// return Context.Deleteable<T>().In(obj).ExecuteCommand();
|
||||
//}
|
||||
public int Delete(object id)
|
||||
{
|
||||
return Context.Deleteable<T>(id).ExecuteCommand();
|
||||
@@ -289,20 +252,20 @@ namespace ZR.Repository
|
||||
return Context.Queryable<T>();
|
||||
}
|
||||
|
||||
public ISugarQueryable<ExpandoObject> Queryable(string tableName, string shortName)
|
||||
{
|
||||
return Context.Queryable(tableName, shortName);
|
||||
}
|
||||
//public ISugarQueryable<ExpandoObject> Queryable(string tableName, string shortName)
|
||||
//{
|
||||
// return Context.Queryable(tableName, shortName);
|
||||
//}
|
||||
|
||||
public List<T> QueryableToList(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
return Context.Queryable<T>().Where(expression).ToList();
|
||||
}
|
||||
|
||||
public Task<List<T>> QueryableToListAsync(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
return Context.Queryable<T>().Where(expression).ToListAsync();
|
||||
}
|
||||
//public Task<List<T>> QueryableToListAsync(Expression<Func<T, bool>> expression)
|
||||
//{
|
||||
// return Context.Queryable<T>().Where(expression).ToListAsync();
|
||||
//}
|
||||
|
||||
//public string QueryableToJson(string select, Expression<Func<T, bool>> expressionWhere)
|
||||
//{
|
||||
@@ -310,45 +273,45 @@ namespace ZR.Repository
|
||||
// return query.JilToJson();
|
||||
//}
|
||||
|
||||
public List<T> QueryableToList(string tableName)
|
||||
{
|
||||
return Context.Queryable<T>(tableName).ToList();
|
||||
}
|
||||
//public List<T> QueryableToList(string tableName)
|
||||
//{
|
||||
// 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> 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;
|
||||
var list = Context.Queryable<T>().Where(expression).ToPageList(pageIndex, pageSize, ref totalNumber);
|
||||
return (list, totalNumber);
|
||||
}
|
||||
//public (List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, int pageIndex = 0, int pageSize = 10)
|
||||
//{
|
||||
// int totalNumber = 0;
|
||||
// var list = Context.Queryable<T>().Where(expression).ToPageList(pageIndex, pageSize, ref totalNumber);
|
||||
// return (list, totalNumber);
|
||||
//}
|
||||
|
||||
public (List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, string order, int pageIndex = 0, int pageSize = 10)
|
||||
{
|
||||
int totalNumber = 0;
|
||||
var list = Context.Queryable<T>().Where(expression).OrderBy(order).ToPageList(pageIndex, pageSize, ref totalNumber);
|
||||
return (list, totalNumber);
|
||||
}
|
||||
//public (List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, string order, int pageIndex = 0, int pageSize = 10)
|
||||
//{
|
||||
// int totalNumber = 0;
|
||||
// var list = Context.Queryable<T>().Where(expression).OrderBy(order).ToPageList(pageIndex, pageSize, ref totalNumber);
|
||||
// return (list, totalNumber);
|
||||
//}
|
||||
|
||||
public (List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, Expression<Func<T, object>> orderFiled, string orderBy, int pageIndex = 0, int pageSize = 10)
|
||||
{
|
||||
int totalNumber = 0;
|
||||
//public (List<T>, int) QueryableToPage(Expression<Func<T, bool>> expression, Expression<Func<T, object>> orderFiled, string orderBy, int pageIndex = 0, int pageSize = 10)
|
||||
//{
|
||||
// int totalNumber = 0;
|
||||
|
||||
if (orderBy.Equals("DESC", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var list = Context.Queryable<T>().Where(expression).OrderBy(orderFiled, OrderByType.Desc).ToPageList(pageIndex, pageSize, ref totalNumber);
|
||||
return (list, totalNumber);
|
||||
}
|
||||
else
|
||||
{
|
||||
var list = Context.Queryable<T>().Where(expression).OrderBy(orderFiled, OrderByType.Asc).ToPageList(pageIndex, pageSize, ref totalNumber);
|
||||
return (list, totalNumber);
|
||||
}
|
||||
}
|
||||
// if (orderBy.Equals("DESC", StringComparison.OrdinalIgnoreCase))
|
||||
// {
|
||||
// var list = Context.Queryable<T>().Where(expression).OrderBy(orderFiled, OrderByType.Desc).ToPageList(pageIndex, pageSize, ref totalNumber);
|
||||
// return (list, totalNumber);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var list = Context.Queryable<T>().Where(expression).OrderBy(orderFiled, OrderByType.Asc).ToPageList(pageIndex, pageSize, ref totalNumber);
|
||||
// return (list, totalNumber);
|
||||
// }
|
||||
//}
|
||||
|
||||
public List<T> SqlQueryToList(string sql, object obj = null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user