0

Folks,

Envrionment: ASP.NET MVC 4, Razor

I am using SimpleMembership provider for my web application. When the user requests for registration, I need to call WebSecurity.CreateUserAndAccount and also update some other tables. The whole operation has to be transactional. Here is what I am thinking:

using (UsersContext ctx = new UsersContext()) {
    using (TransactionScope scope = new TransactionScope()) {

        // The following call creates its own context but will call ctx.SaveChanges() internally
        WebSecurity.CreateUserAndAccount(...);

        // update some other tables using ctx
        ...

        ctx.SaveChanges();

        scope.complete();
    }
}

I have a feeling this should work. However, I would like to get your expert opinion on whether there is a better way.

Thank you in advance for your help.

Regards,
Peter

Peter
  • 11,260
  • 14
  • 78
  • 155

1 Answers1

0

TransactionScope is the best way to make it all happen inside the same transaction, but be careful with how your connection strings are defined (and test on Azure if that's where you are deploying it to) as the DTC may get involved and cause you problems in some scenarios (example)

The alternative would be to inherit from SimpleMembershipProvider and override CreateUserAndAccount, as that is all that WebSecurity calls. You could then do all the work inside a single context by duplicating and extending the SimpleMembershipProvider code.

Community
  • 1
  • 1
Andy Brown
  • 18,961
  • 3
  • 52
  • 62