我可以使用 ADO NET 实体数据模型创建事务吗?

发布于 2024-09-01 03:06:22 字数 782 浏览 7 评论 0原文

是否可以在以下 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();
    }

}

任何想法将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来日方长 2024-09-08 03:06:22
using (var transaction = db.Connection.BeginTransaction())
{
    // your code here
    ...


    transaction.Commit();
}
using (var transaction = db.Connection.BeginTransaction())
{
    // your code here
    ...


    transaction.Commit();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文