1

是否可以在以下 try-catch 上使用 ADO NET 实体数据模型将一组语句作为事务执行?

[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();
    }

}

任何想法将不胜感激。

4

1 回答 1

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


    transaction.Commit();
}
于 2010-05-13T22:59:44.353 回答