コード例 #1
0
        public IHttpActionResult PostTransaction(TransactionsModel transaction)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var dbPostTransaction = new Transaction();

            dbPostTransaction.Update(transaction);

            dbPostTransaction.Add(transaction);

            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = dbPostTransaction.TransactionID }, transaction);
        }
コード例 #2
0
        public IHttpActionResult PostTransaction(TransactionModel transaction)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            // Set up new Transactio object,
            //  and populate it with the values from
            //  the input TransactionModel object
            Transaction dbTransaction = new Transaction();
            dbTransaction.Update(transaction);

            // Add the new Transaction object to the list of Transaction objects
            db.Transactions.Add(dbTransaction);

            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {

                throw new Exception("Unable to add the transaction to the database.");
            }

            // Update the TransactionModel object with the new transaction ID
            //  that was placed in the Transaction object after the changes
            //  were saved to the DB
            transaction.TransactionId = dbTransaction.TransactionId;
            return CreatedAtRoute("DefaultApi", new { id = dbTransaction.TransactionId }, transaction);
        }