How to implement transactions in an application based on MVC4 / EF 6+

asp.net-mvc asp.net-mvc-4 c# entity-framework transactions

Question

I recently read about 5-Zzz. It is really helpful and excellent.

I first questioned if I actually needed transactions in MVC 4 and EF 6+. The reason is that we consistently invokeDbContext.SaveChanges() saving modifications. I'm curious ifSaveChanges() is something that replicates the close of a transaction, so if I invokeSaveChanges() I enter into an agreement.

However, how can I build transactions if I require them?TransactionScope when using EF/MVC applications. My circumstance resembles the following steps:

  • I store accurate records in databases.
  • I save copies of both new and old records in another table that serves as a sort of archive for the original table.
  • I record user activities in a different table.

I also offered some code. You can see that my data is inconsistent if something goes wrong in the middle. If you could provide me some examples on how to useTransactionScope . Additionally, I might need more to save in other tables. I want to make sure that I either save everything or nothing, saving everything if the transaction is successful or rolling back everything that occurred before to the issue.

Thanks.

[HttpPost]
public ActionResult Edit(ApplicationViewModel viewmodel)
{
    using(MyDbCOntext dbContext = new MyDbContext())
    {
        if(!MoselState.IsValid)
            return View(application);

        // Copy old data from database and assign to an object
        ApplicationArchive applicationOld = CopyApplicationFromDB(db, viewmodel.ApplicationID);

        // Update model
        if (TryUpdateModel(applicationNew), null, null, new string[] { "ApplicationID" })
        {
            try
            {
                dbContext.Entry(userToUpdate).State = EntityState.Modified;
                dbContext.SaveChanges();

                // Archive old application
                ApplicationArchive applicationNew = CopyApplicationFromDB(db, viewmodel.ApplicationID);
                try
                {
                    dbContext.ApplicationsArchive.Add(applicationOld);
                    dbCOntext.ApplicationsArchive.Add(applicationNew);
                    dbContext.SaveChanges();

                    // Register user activity
                    string username = GetUserNameFromCookie();
                    UserActivity useractivity = new UserActivity() { UserName = username, activity = "edit", Table = "application" };
                    try
                    {
                        dbContext.UserActivities.Add(useractivity);
                        dbContext.SaveChanges();

                        return RedirectView("Index");
                    }
                }
            }
            catch
            {
                ModelState.AddModelError("", "Cannot update this application");
            }
        }


        //

        return View(application);
    }
}
1
1
2/4/2015 6:44:25 AM

Accepted Answer

ZZZ_tmp
3
2/4/2015 8:08:40 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow