2010-05-13 84 views
1

是否有可能在下面的try-catch中使用ADO NET實體數據模型執行一組語句作爲事務我可以使用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(); 
    } 

} 

任何想法將不勝感激。

回答

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


    transaction.Commit(); 
} 
相關問題