protected DbOperationStatus Save()
        {
            var opStatus = new DbOperationStatus
            {
                OperationSuccessStatus = false,
                AffectedIndices        = null
            };

            try
            {
                opStatus.OperationSuccessStatus = DataContext.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                opStatus = DbOperationStatus.CreateFromException("Error saving.", ex);
            }
            return(opStatus);
        }
        public static DbOperationStatus CreateFromException(string statusMessage, Exception ex)
        {
            var opStatus = new DbOperationStatus
            {
                OperationSuccessStatus = false,
                Message         = statusMessage,
                AffectedIndices = null
            };

            if (ex != null)
            {
                opStatus.ExceptionMessage         = ex.Message;
                opStatus.ExceptionStackTrace      = ex.StackTrace;
                opStatus.ExceptionInnerMessage    = (ex.InnerException != null) ? ex.InnerException.Message : null;
                opStatus.ExceptionInnerStackTrace = (ex.InnerException != null) ? ex.InnerException.StackTrace : null;
            }

            return(opStatus);
        }
        public static DbOperationStatus CreateFromException(string statusMessage, Exception ex)
        {
            var opStatus = new DbOperationStatus
            {
                OperationSuccessStatus = false,
                Message = statusMessage,
                AffectedIndices = null
            };

            if (ex != null)
            {
                opStatus.ExceptionMessage = ex.Message;
                opStatus.ExceptionStackTrace = ex.StackTrace;
                opStatus.ExceptionInnerMessage = (ex.InnerException != null) ? ex.InnerException.Message : null;
                opStatus.ExceptionInnerStackTrace = (ex.InnerException != null) ? ex.InnerException.StackTrace : null;
            }

            return opStatus;
        }
        protected DbOperationStatus DeleteEntity <TE>(TE entity) where TE : class
        {
            var opStatus = new DbOperationStatus
            {
                OperationSuccessStatus = false,
                AffectedIndices        = null
            };

            try
            {
                DataContext.Set <TE>().Remove(entity);
                opStatus.OperationSuccessStatus = DataContext.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                opStatus = DbOperationStatus.CreateFromException("Error deleting " + entity.GetType(), ex);
            }
            return(opStatus);
        }
        protected DbOperationStatus Insert(T entity)
        {
            var opStatus = new DbOperationStatus
            {
                OperationSuccessStatus = false,
                AffectedIndices        = null
            };

            try
            {
                DataContext.Set <T>().Add(entity);
                opStatus.OperationSuccessStatus = DataContext.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                opStatus = DbOperationStatus.CreateFromException("Error inserting " + entity.GetType(), ex);
            }
            return(opStatus);
        }
        protected DbOperationStatus UpdateEntity <TE>(TE entity) where TE : class
        {
            var opStatus = new DbOperationStatus
            {
                OperationSuccessStatus = false,
                AffectedIndices        = null
            };

            try
            {
                DataContext.Set <TE>().Attach(entity);
                DataContext.Entry(entity).State = EntityState.Modified;
                opStatus.OperationSuccessStatus = DataContext.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                opStatus = DbOperationStatus.CreateFromException("Error updating " + entity.GetType(), ex);
            }
            return(opStatus);
        }
        public DbOperationStatus ExecuteStoredProcedure(string command)
        {
            if (command == null)
            {
                throw new ArgumentException("The stored procedure must be provided");
            }

            var opStatus = new DbOperationStatus {
                OperationSuccessStatus = false
            };

            try
            {
                opStatus.OperationSuccessStatus = DataContext.Database.ExecuteSqlCommand(command) > 0;
            }
            catch (Exception ex)
            {
                opStatus = DbOperationStatus.CreateFromException("Error executing Stored Procedure", ex);
            }

            return(opStatus);
        }