Ejemplo n.º 1
0
        public SaveResult SaveChanges(JObject saveBundle, TransactionSettings transactionSettings = null)
        {
            JsonSerializer = CreateJsonSerializer();

            var dynSaveBundle  = (dynamic)saveBundle;
            var entitiesArray  = (JArray)dynSaveBundle.entities;
            var dynSaveOptions = dynSaveBundle.saveOptions;

            SaveOptions   = (SaveOptions)JsonSerializer.Deserialize(new JTokenReader(dynSaveOptions), typeof(SaveOptions));
            SaveWorkState = new SaveWorkState(this, entitiesArray);

            transactionSettings = transactionSettings ?? BreezeConfig.Instance.GetTransactionSettings();
            try {
                if (transactionSettings.TransactionType == TransactionType.TransactionScope)
                {
                    var txOptions = transactionSettings.ToTransactionOptions();
                    using (var txScope = new TransactionScope(TransactionScopeOption.Required, txOptions)) {
                        OpenAndSave(SaveWorkState);
                        txScope.Complete();
                    }
                }
                else if (transactionSettings.TransactionType == TransactionType.DbTransaction)
                {
                    this.OpenDbConnection();
                    using (IDbTransaction tran = BeginTransaction(transactionSettings.IsolationLevelAs)) {
                        try {
                            OpenAndSave(SaveWorkState);
                            tran.Commit();
                        } catch {
                            tran.Rollback();
                            throw;
                        }
                    }
                }
                else
                {
                    OpenAndSave(SaveWorkState);
                }
            } catch (EntityErrorsException e) {
                // SaveWorkState.EntityErrors = e.EntityErrors;
                var error = new SaveError(e.EntityErrors);
                var resp  = new HttpResponseMessage(e.StatusCode)
                {
                    Content      = new ObjectContent(typeof(SaveError), error, JsonFormatter.Create()),
                    ReasonPhrase = e.Message ?? "Entity Errors exception"
                };
                throw new HttpResponseException(resp);
            } catch (Exception e2) {
                if (!HandleSaveException(e2, SaveWorkState))
                {
                    throw;
                }
            } finally {
                CloseDbConnection();
            }

            return(SaveWorkState.ToSaveResult());
        }
    public SaveResult SaveChanges(JObject saveBundle, TransactionSettings transactionSettings = null) {
      JsonSerializer = CreateJsonSerializer();

      var dynSaveBundle = (dynamic)saveBundle;
      var entitiesArray = (JArray)dynSaveBundle.entities;
      var dynSaveOptions = dynSaveBundle.saveOptions;
      SaveOptions = (SaveOptions) JsonSerializer.Deserialize(new JTokenReader(dynSaveOptions), typeof(SaveOptions));
      SaveWorkState = new SaveWorkState(this, entitiesArray);

      transactionSettings = transactionSettings ?? BreezeConfig.Instance.GetTransactionSettings();
      try {
        if (transactionSettings.TransactionType == TransactionType.TransactionScope) {
          var txOptions = transactionSettings.ToTransactionOptions();
          using (var txScope = new TransactionScope(TransactionScopeOption.Required, txOptions)) {           
            OpenAndSave(SaveWorkState);           
            txScope.Complete();
          }
        } else if (transactionSettings.TransactionType == TransactionType.DbTransaction) {
          this.OpenDbConnection();
          using (IDbTransaction tran = BeginTransaction(transactionSettings.IsolationLevelAs)) {
            try {
              OpenAndSave(SaveWorkState);
              tran.Commit();
            } catch {
              tran.Rollback();
              throw;
            }
          }          
        } else {
          OpenAndSave(SaveWorkState);
        }
      } catch (EntityErrorsException e) {
        // SaveWorkState.EntityErrors = e.EntityErrors;
        var error = new SaveError(e.EntityErrors);
        var resp = new HttpResponseMessage(e.StatusCode) {
          Content = new ObjectContent(typeof(SaveError), error, JsonFormatter.Create()),
          ReasonPhrase = e.Message ?? "Entity Errors exception"
        };
        throw new HttpResponseException(resp);
      } catch(Exception e2) {
        if (!HandleSaveException(e2, SaveWorkState)) {
          throw;
        }
      } finally {
        CloseDbConnection();
      }

      return SaveWorkState.ToSaveResult();

    }