0

伙计们,

环境:ASP.NET MVC 4,Razor

我正在为我的 Web 应用程序使用 SimpleMembership 提供程序。当用户请求注册时,我需要调用 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();
    }
}

我觉得这应该可行。但是,我想就是否有更好的方法征求您的专家意见。

预先感谢您的帮助。

问候,
彼得

4

1 回答 1

0

TransactionScope是让这一切发生在同一个事务中的最佳方式,但请注意连接字符串的定义方式(如果将其部署到 Azure,请在 Azure 上进行测试),因为 DTC 可能会涉及并导致您在某些方面出现问题场景(示例

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

于 2013-10-22T16:00:59.910 回答