Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Entity Framework - Object Services Level. Transactions.
Mike Taulty's Blog

Mike's Badges

Follow on Twitter
View mike's profile on slideshare
Add to Technorati Favorites
CW Blog Awards

How do transactions play with the Entity Framework? As a starting point, if I do something like;

  static void Main(string[] args)
    {
      using (NorthwindContext ctx = new NorthwindContext("Name=NorthwindEntities"))
      {
        foreach (Shippers s in ctx.Shippers)
        {
          s.Phone = "Bar";
        }
        ctx.SaveChanges(true);
      }
    }

Then, from the SQL Profiler I can see that a transaction is being created for me by SaveChanges and is committed at the point where SaveChanges succeeds (it'll be rolled back in the case where SaveChanges fails).

Here's the complete profiler trace;

image

So, that's reasonably obvious - the query executes on its own and then the updates are all bounded for me by a transaction created by the SaveChanges method. If I wanted the transaction to also include the query then I could use a TransactionScope to control that. The chances are that I'd be doing that because I wanted to change the transaction isolation level so I could do something like;

static void Main(string[] args)
    {
      using (NorthwindContext ctx = new NorthwindContext("Name=NorthwindEntities"))
      {
        TransactionOptions options = new TransactionOptions() 
        { 
          IsolationLevel = System.Transactions.IsolationLevel.Serializable 
        };
        using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, options))
        {
          foreach (Shippers s in ctx.Shippers)
          {
            s.Phone = "Foo";
          }
          ctx.SaveChanges(true);

          Console.WriteLine("Commit (c) or rollback (b)?");

          if (Console.ReadLine() == "c")
          {
            scope.Complete();
          }
        }
      }
    }
giving a profiler output like;

 

image 

And so now I've got my Serializable transaction spanning from before the initial query until after the call to SaveChanges. Note that also the SaveChanges is smart enough to realise that because it did not create the transaction then it has no real right trying to commit it and so it doesn't commit it. It leaves that decision to the calling code and (in this instance) I pressed "b" and rolled it back as you can see in the trace.

One last point here that I'm not very sure about at all...

What if you've already got a transaction kicking around somewhere and you want to bring some Entity Framework code into it? It's easy enough if you've got an existing System.Transactions.Transaction because you can just grab that from Transaction.Current but what if you have (e.g.) a SqlTransaction? That is, you've written some code against SqlClient in the past and you've passed around a SqlTransaction or an IDbTransaction explicitly and now you're writing a new bit of code with Entity Framework and you want to plug in to that IDbTransaction that you've got?

At the time of writing, I'm not sure whether you can do this or not. Some things look promising;

  1. EntityConnection.Transaction looks promising until I figured out it's a read-only property for an EntityTransaction.
  2. EntityConnection.EnlistTransaction looks promising until I figured out that it wants a System.Transactions.Transaction.

So, I'm not 100% as to how you do this. In the LINQ to SQL world, it's easier because you just set the DataContext.Transaction property. However, it's seems like it should be a lot easier for the LINQ to SQL case because it knows to expect a SqlTransaction whereas the Entity Framework can't deal with a specific transaction type (although it could perhaps accept an IDbTransaction?).

So, that last point remains a big "TBD" for me - not sure how/whether you can do this but I'll update the post if I find out one way or another.


Posted Mon, Aug 27 2007 4:49 PM by mtaulty

Comments

Guy Burstein's Blog wrote ADO.Net Entity Framework Beta 2 is available
on Mon, Aug 27 2007 10:36 PM
ADO.Net Entity Framework Beta 2 is available The ADO.Net Entity Framework bits for Visual Studio 2008
Mike Taulty's Blog wrote ADO.NET Entity Framework - Bringing Together A Few Previous Posts
on Wed, Aug 29 2007 5:38 PM
This is just a convenience - links to the posts that I've made so far around beta 2 of the ADO.NET Entity...
Entity Framework from Mike Taulty « vincenthome’s Software Development wrote Entity Framework from Mike Taulty « vincenthome’s Software Development
on Fri, Sep 7 2007 7:25 AM
(C) Mike Taulty, 2009. All rights reserved. The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems