开发者

Can SubSonic's SimpleRepository enlist in a transaction for two different object types?

I've been exploring Sub Sonic 3's SimpleRepository and have been pretty happy with it but have a question regarding transactions. I am aware that using methods like 'AddMany' and 'DeleteMany' will automatically perform all of those operations within a single transaction, but was wondering if it's possible to force the SimpleRepository to perform the add or update of two different object types within the same transaction. For example, let's say I have the notion of two different but related entities in my application: a User and a Profile. Every user has to have a Profile and every Profile belongs to one and only one user. When a new user registers with my application I want them to provide the basic user information (credentials, name, e-mail) but also want some additional "profile" information (about me, gender, zip code, etc.) I'd like to be able to perform the add of the User and the Profile object within a single transaction, but since it requires two distinct calls to the 'Add' method with d开发者_开发知识库ifferent type parameters I'm not sure how to make this work.


You can do this using a transaction as follows:

  using (TransactionScope transactionScope = new TransactionScope())
  {
    using (SharedDbConnectionScope connectionScope = new SharedDbConnectionScope())
    {
      // Add your user
      // Add your profile

      transactionScope.Complete();
    }
  }


adam - i think the code above is correct but is actually the wrong way around, it should be:

  using (SharedDbConnectionScope connectionScope = new SharedDbConnectionScope())
  {
    using (TransactionScope transactionScope = new TransactionScope())
    {
      // Add your user
      // Add your profile

      transactionScope.Complete();
    }
  }

cheers jimi

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜