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 i.e 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.