Пример #1
0
        public async Task <DatabaseOneResult <TEntity> > FindOneAsync(Expression <Func <TEntity, bool> > where, Expression <Func <TEntity, object> > include = null, Expression <Func <TEntity, object> > include2 = null, Expression <Func <TEntity, object> > include3 = null, Expression <Func <TEntity, object> > include4 = null)
        {
            var dbResult = new DatabaseOneResult <TEntity>();

            try
            {
                var dbSet = _applicationDbContext.Set <TEntity>();

                var dbQuery = dbSet.AsQueryable();
                dbQuery = (include != null) ? dbQuery.Include(include) : dbQuery;
                dbQuery = (include2 != null) ? dbQuery.Include(include2) : dbQuery;
                dbQuery = (include3 != null) ? dbQuery.Include(include3) : dbQuery;
                dbQuery = (include4 != null) ? dbQuery.Include(include4) : dbQuery;

                var result = await dbQuery.FirstOrDefaultAsync(where);

                if (result == null)
                {
                    throw new ArgumentNullException("Result object is null.");
                }

                dbResult.Success = true;
                dbResult.Entity  = result;
                return(dbResult);
            }
            catch (Exception ex)
            {
                dbResult.Exception = ex;
                dbResult.Message   = "Exception FindOneAsync " + typeof(TEntity).Name;
                return(dbResult);
            }
        }
        public async Task <DatabaseOneResult <TEntity> > FindOneAsync(string id)
        {
            var dbResult = new DatabaseOneResult <TEntity>();

            try
            {
                var dbSet  = _applicationDbContext.Set <TEntity>();
                var result = await dbSet.FindAsync(id);

                if (result == null)
                {
                    throw new ArgumentNullException("Result object is null.");
                }

                dbResult.Success = true;
                dbResult.Entity  = result;
                return(dbResult);
            }
            catch (Exception ex)
            {
                dbResult.Exception = ex;
                dbResult.Message   = "Exception FindOneAsync " + typeof(TEntity).Name;
                return(dbResult);
            }
        }
Пример #3
0
        public async Task <DatabaseOneResult <TEntity> > SqlQueryEntityAsync(string query)
        {
            var dbResult = new DatabaseOneResult <TEntity>();

            try
            {
                var result = await _applicationDbContext.Database.SqlQuery <TEntity>(query).FirstOrDefaultAsync();

                if (result == null)
                {
                    throw new ArgumentNullException("Result object is null.");
                }

                dbResult.Success = true;
                dbResult.Entity  = result;
                return(dbResult);
            }
            catch (Exception ex)
            {
                dbResult.Exception = ex;
                dbResult.Message   = "Exception SqlQueryEntityAsync " + typeof(TEntity).Name;
                return(dbResult);
            }
        }