示例#1
0
        public async Task <TEntity> FirstOrDefaultAsync(Expression <Func <TEntity, bool> > predicate, bool dataPermission = true)
        {
            try
            {
                using (var conn = GetDbConnection())
                {
                    if (conn.State != System.Data.ConnectionState.Open)
                    {
                        conn.Open();
                    }

                    predicate = _queryFilter.ExecuteFilter <TEntity, TPrimaryKey>(predicate);
                    if (dataPermission)
                    {
                        predicate = _orgQueryFilter.ExecuteFilter <TEntity, TPrimaryKey>(predicate);
                    }

                    var pg     = predicate.ToPredicateGroup <TEntity, TPrimaryKey>();
                    var result = conn.GetList <TEntity>(pg).FirstOrDefault();
                    return(result);
                }
            }
            catch (Exception ex)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                {
                    _logger.LogError(ex.Message, ex);
                }

                throw new DataAccessException(ex.Message, ex);
            }
        }