Exemplo n.º 1
0
        public DataAccessResult Modify(TEntity entity)
        {
            var result = new DataAccessResult();

            try
            {
                DbContext.Set <TEntity>().Attach(entity);
                DbContext.Entry(entity).State = EntityState.Modified;

                if (DbContext.SaveChanges() > 0)
                {
                    result.ResultCode = ResultCodeOption.Ok;
                    result.Message    = DataAccessResult.SuccessDefaultString;
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                LogHelper.Log.Error("Modify(TEntity entity) exception.", dbEx);
                result.ResultCode = ResultCodeOption.ValidationFailure;
                result.Message    = dbEx.InnerException?.InnerException?.Message;
            }
            catch (DbUpdateException dbEx)
            {
                LogHelper.Log.Error("Modify(TEntity entity) exception.", dbEx);
                result.ResultCode = ResultCodeOption.Duplicate;
                result.Message    = dbEx.InnerException?.InnerException?.Message;
            }
            catch (Exception dbEx)
            {
                LogHelper.Log.Error("Modify(TEntity entity) exception.", dbEx);
                result.Message = dbEx.GetType().ToString();
            }

            return(result);
        }
Exemplo n.º 2
0
        public DataAccessResult Delete(int id)
        {
            var result = new DataAccessResult();

            TEntity entity;

            try
            {
                entity = DbContext.Set <TEntity>().Find(id);
            }
            catch (Exception dbEx)
            {
                LogHelper.Log.Error("LoadSingleTuple(int id) exception.", dbEx);
                entity = null;
            }

            if (entity == null)
            {
                result.ResultCode = ResultCodeOption.Ok;
                result.Message    = "The specified tuple is not exist.";
                return(result);
            }

            try
            {
                DbContext.Set <TEntity>().Attach(entity);
                DbContext.Set <TEntity>().Remove(entity);

                if (DbContext.SaveChanges() > 0)
                {
                    result.ResultCode = ResultCodeOption.Ok;
                    result.Message    = DataAccessResult.SuccessDefaultString;
                }
            }
            catch (Exception dbEx)
            {
                result.ResultCode = ResultCodeOption.Failure;
                result.Message    = dbEx.GetType().ToString();
            }

            return(result);
        }
Exemplo n.º 3
0
        public DataAccessResult Add(TEntity entity)
        {
            var result = new DataAccessResult();

            try
            {
                DbContext.Set <TEntity>().Add(entity);

                if (DbContext.SaveChanges() > 0)
                {
                    result.ResultCode = ResultCodeOption.Ok;
                    result.Message    = DataAccessResult.SuccessDefaultString;
                }
            }
            catch (Exception dbEx)
            {
                LogHelper.Log.Error("Add(TEntity entity) exception.", dbEx);
                result.Message = dbEx.GetType().ToString();
            }

            return(result);
        }
Exemplo n.º 4
0
        public DataAccessResult Delete(TEntity entity)
        {
            var result = new DataAccessResult();

            DbContext.Set <TEntity>().Attach(entity);
            DbContext.Entry(entity).State = EntityState.Deleted;
            //DbContext.Set<TEntity>().Remove(entity);

            try
            {
                if (DbContext.SaveChanges() > 0)
                {
                    result.ResultCode = ResultCodeOption.Ok;
                    result.Message    = DataAccessResult.SuccessDefaultString;
                }
            }
            catch (Exception dbEx)
            {
                LogHelper.Log.Error("Delete(TEntity entity) exception.", dbEx);
                result.Message = dbEx.GetType().ToString();
            }

            return(result);
        }