public T Get <T>(Expression <Func <T, bool> > expression) where T : BaseModel, new() { Type type = typeof(T); string sqlWhere = expression != null?_expressionToSqlWhereHelper.Condition(expression) : ""; sqlWhere = string.IsNullOrWhiteSpace(sqlWhere) ? "" : $" where {sqlWhere}"; string columsString = string.Join(",", type.GetProperties().Select(p => $"[{p.Name}]")); string sqlStr = $"select {columsString} from [{type.Name}] {sqlWhere}"; IDbConnection conStr = _ConnectionFactory.GetConnection(WriteAndRead.Read); using (SqlConnection conn = new SqlConnection(conStr.ConnectionString)) { var r = conn.Query <T>(sqlStr).ToList(); if (r != null && r.Count > 0) { return(r[0] as T); } } return(default(T)); }
/// <summary> /// 单个查询 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="dbTransaction"></param> /// <param name="buffered">缓冲/缓存</param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <returns></returns> public IEnumerable <T> Query <T>(string sql, object param = null, IDbTransaction dbTransaction = null, bool buffered = true, int?commandTimeout = null, CommandType?commandType = null) where T : BaseEntity { return(_customConnectionFactory.GetConnection(DBExcuteOption.Read).Query <T>(sql, param, dbTransaction, buffered, commandTimeout, commandType)); //return ConnectionOptions.DbConnection.Query<T>(sql, param, dbTransaction,buffered, commandTimeout, commandType); }
public DapperHelper(ICustomConnectionFactory customConnectionFactory) { _customConnectionFactory = customConnectionFactory; dbConnection = _customConnectionFactory.GetConnection(DBExcuteOption.Read); }