views:

169

answers:

1

Hi

is it possible on the following try-catch to execute a set of statements as a transaction using ADO NET Entity Data Model?

[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Customer c)
{   
    try
    {
        c.Created = DateTime.Now;
        c.Active = true;
        c.FullName = Request.Form["FirstName"];
        db.AddToCustomer(c);
        db.SaveChanges();

        Log log = new Log();//another entity model object
        log.Created = DateTime.Now;
        log.Message = 
            string.Format(@"A new customer was created 
            with customerID {0}", c.CustomerID);
        db.AddToLog(log);

        db.SaveChanges();
        return RedirectToAction("CreateSuccess", "Customer");
    }
    catch
    {
        return View();
    }

}

Any thoughts would be very appreciated.

A: 
using (var transaction = db.Connection.BeginTransaction())
{
    // your code here
    ...


    transaction.Commit();
}
Thomas Levesque