public static OperationResult CreateFromException(string message, Exception ex) { var operationResult = new OperationResult { Status = false, Message = message, OperationId = null }; if (ex != null) { operationResult.ExceptionMessage = ex.Message; operationResult.ExceptionStackTrace = ex.StackTrace; operationResult.ExceptionInnerMessage = (ex.InnerException == null) ? null : ex.InnerException.Message; operationResult.ExceptionInnerStackTrace = (ex.InnerException == null) ? null : ex.InnerException.StackTrace; } return operationResult; }
public OperationResult Commit <T>(List <T> disconnectedEntities = null) where T : class, IObjectWithState { OperationResult result; using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted })) { if (disconnectedEntities != null) { foreach (var customer in disconnectedEntities) { if (customer.State != State.Unchanged) { DbContext.Set <T>().Add(customer); } } } DbContext.ApplyStateChanges(); try { DbContext.SaveChanges(); result = new OperationResult { Status = true }; } catch (DbUpdateConcurrencyException ex) { // Optimistic Concurrency reloads data from Database. if (disconnectedEntities != null) { foreach (var entity in disconnectedEntities) { if (entity.State != State.Unchanged) { DbContext.Entry(entity).CurrentValues.SetValues(DbContext.Entry(entity).GetDatabaseValues()); } } } else { foreach (var entity in DbContext.Set <T>()) { if (entity.State != State.Unchanged) { DbContext.Entry(entity).CurrentValues.SetValues(DbContext.Entry(entity).GetDatabaseValues()); } } } result = OperationResult.CreateFromException("Concurrency Exception occured!", ex); result.ResultType = OperationResultType.ConcurencyExeption; } finally { DbContext.ResetEntityStates(); scope.Complete(); } } return(result); }