包装上的单笔交易成员提供和的DbContext [英] Wrapping membership provider and dbcontext on single transaction

查看:279
本文介绍了包装上的单笔交易成员提供和的DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC项目使用EF 5 code第一和.NET 4.5。

MVC Project using EF 5 code first and .NET 4.5.

我一直在寻找一种方式来包装的DbContext和SimpleMembershipProvider在一个单独的事务。我试图用的TransactionScope,但由于成员资格提供将打开另一个方面,我得到一个异常(MSDTC在服务器ServerName不可用)。

I was looking for a way to wrap dbContext and SimpleMembershipProvider on a single transaction. I tried to use TransactionScope but since the membership provider will open another connection I get an exception (MSDTC on server 'servername' is unavailable).

所以我虽然我可以用ObjectContext.Connection.BeginTransaction来代替。
成员资格提供程序不会成为交易的一部分,但这个想法是,如果失败,该交易将不被承诺有它的地方在哪里。

So I though I could use ObjectContext.Connection.BeginTransaction instead. The membership provider won't be part of the transaction but the idea is to have it somewhere where if it fails the transaction won't be committed.

Book bk1 = default(Book);
Book bk2 = default(Book);

object obc = (IObjectContextAdapter)dbContext;

obc.ObjectContext.Connection.Open();
using (tx == obc.ObjectContext.Connection.BeginTransaction) {

    bk1 = new Book {
        Name = "Book 1 Name",
        Location = "USA"
    };
    dbContext.Books.Add(bk1);
    dbContext.SaveChanges();
    bk2 = new Book {
        Name = "Book 2 Name. Book one Id is: " + bk1.Id,
        Location = "USA"
    };
    dbContext.Books.Add(bk2);
    dbContext.SaveChanges();

    // this is not part of the transaction, 
    // however if it trhows an exception the transaction is aborted.
    // I'm assuming that if we got here, the commit won't fail...    
    // But is it really true?
    WebSecurity.CreateUserAndAccount("username", "123456");

    tx.Commit();
}

不管怎么说,基于上述code:

Anyways, based on the above code:


  • 如果WebSecurity.CreateUserAndAccount失败,则整个事情失败,预计。

  • 如果任何SaveChanges方法失败,再次对整个事情,因为我们不明白的地方执行CreateUserAndAccount点失败。

这整个事情给我带来了一个问题:
是否安全?我的意思是:

This whole thing bring me to a question: Is it safe? What I mean is:

是否有可能在提交的方法将抛出一个异常(失败不知何故)后,我们成功地执行DbContext.SaveChanges?如果它发生,我们会用一个孤儿用户endup因为提供者不是交易的一部分。

Is there any possibility the "Commit" method will throw an exception(of fail somehow) after we successfully execute DbContext.SaveChanges ? If it happens we will endup with an orphan user because the provider is not part of the transaction.

我AP preciate对此有任何意见或建议。

I appreciate any comments or advice on this.

快速注:
有这样好的文章,解释为什么我投的DbContext到IObjectContextadapter而不是使用自己的连接属性,但我无法找到它了。

推荐答案

提交当然可以抛出。一个简单的例子是,如果连接断开。还有其他的情况下,当然。

Yes, Commit can certainly throw. One trivial example is if the connection drops. There are other cases, of course.

您有几种选择:


    在相同的DB
  • 交易将不会升级为分布式如果连接是共享的。所以,你可以使用这两个EF和你的 WebSecurity 连接一个连接。

  • 在服务器上启动分布式事务控制器和升级居住。没有世界上最糟糕的事情。

  • ,而不是使用一个事务,改变操作的顺序,使得一个部分成功的操作可以完成或以后撤消。例如,检测和清除孤儿的用户。

  • Transactions on the same DB won't escalate to distributed if the connection is shared. So you could use one connection for both EF and your WebSecurity connection.
  • Start the distributed transaction controller on the server and live with the escalation. Not the worst thing in the world.
  • Instead of using a transaction, change the order of operations such that a partially successful operation can be completed or undone later. E.g., detect and clean up "orphan" users.

这篇关于包装上的单笔交易成员提供和的DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆