2013-09-29 47 views
0

伙计们,SimpleMembership - 正确的方式来创建一个交易

管环境:ASP.NET MVC 4,剃刀

我使用SimpleMembership提供商我的Web应用程序。当用户请求注册时,我需要调用WebSecurity.CreateUserAndAccount并更新一些其他表。整个操作必须是事务性的。这是我在想什么:

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(); 
    } 
} 

我有一种感觉,这应该工作。不过,我想就您是否有更好的方式征求您的专家意见。

非常感谢您的帮助。

问候,
彼得

回答

0

TransactionScope是在Azure上,以使这一切发生在同一个事务中,但要小心你的连接字符串是如何定义的(最好的办法和测试,如果这就是你在哪里部署它到)作为DTC可以参与进来,使你在某些情况下(example

另一种方法是从SimpleMembershipProvider继承和覆盖CreateUserAndAccount问题,因为这是所有WebSecurity电话。然后,您可以通过复制和扩展SimpleMembershipProvider代码在单个上下文中完成所有工作。

相关问题