简体   繁体   中英

Rollingback a transaction in .Net Entity Framework

I have an MVC Action Controller method as:

public ActionResult Register(ViewModel.User model)
    {
        DbContextTransaction dbTransaction = null;
        if (ModelState.IsValid)
        {
            try
            {
                UserAccountLogic registerUser = new UserAccountLogic();

                dbTransaction = registerUser.RegisterUser(model);
                Mail.SendEmail(model.Email, model.FirstName);

                dbTransaction.Commit();
                //Session["Email"] = model.Email;
                ViewBag.Style = "block";
            }
            catch (Exception e)
            {
                dbTransaction.Rollback();
                log.Error(e.Message, e);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

RegisteredUser function is in Business Layer as:

public DbContextTransaction RegisterUser(User model)
    {
        //var userEntity = Mapper.Map<User, Dal.UserInfo>(model);
        var userEntity = MapModelToEntity(model);
        var registerService = new Dal.AccountService();

        return registerService.SaveRegisterDetails(userEntity);
    }

and SaveRegisterDetails function is in DataAccessLayer as:

public DbContextTransaction SaveRegisterDetails(UserInfo registerDetails)
    {
        //TransactionModel transactionModel = new TransactionModel();

        using (HealthCarePortalEntities context = new HealthCarePortalEntities())
        {

            using (DbContextTransaction dbTran = context.Database.BeginTransaction())
            {
                context.UserInfo.Add(registerDetails);
                context.SaveChanges();

                return dbTran;
            }
        }

Now my problem is I want to rollback a transaction ie a newly registered user data from the database when there is any exception in sending activation link to the user. But the problem is the part where I am doing rollback an exception throws because the Database connection is null. So my question is how can I get the connection of DAL layer in Controller which is in another layer. Thanks for the help.

You only need to implement IDisposable

private void Dispose(bool disposing)
{
    // ...
    if (disposing && (this._innerConnection != null))
    {
        this._disposing = true;
        this.Rollback(); // there you go
    }
}

Implement IDisposable

You could use the ambient transaction model.

using(TransactionScope tran = new TransactionScope()) {
     UserAccountLogic registerUser = new UserAccountLogic();

     dbTransaction = registerUser.RegisterUser(model);
     Mail.SendEmail(model.Email, model.FirstName);
     tran.Complete();
}

That way if an exception occurs inside your transaction scope the db rolls back the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM