是否可以在以下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();
}
}
任何想法都会非常感激。
答案 0 :(得分:2)
using (var transaction = db.Connection.BeginTransaction())
{
// your code here
...
transaction.Commit();
}