The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

johnicello (8) [Avatar] Offline
#1
Hi. All in all I think your book is very helpful. But, you should go into more detail about transactions.

For example, with ADO.net (and with NHibernate) you can easily work with transactions and set the Isolation level using the IDbTransaction.IsolationLevel. Is it true that with EntityFramework you need to use TransactionScope and TransactionScopeOptions, which is a network thing that needs to be turned on, both in the calling computer and the database computer?

In my employer's intranet, these options were turned off, MSDTC. Why can't EntityFramework allow you to work with IDbTransaction?

Thanks for any help.
John
stefano.mostarda (47) [Avatar] Offline
#2
Re: Transactions Need More Detail
Hi,

Thanks for your feedback.

TransactionScope doesn't require MSDTC unless you want to use it to put in the same transaction more connection on the same database or more connections on different databases.

In all other cases TransactionScope leverages classic SqlTransaction approach without resorting to MSDTC.

Byez
Stefano
johnicello (8) [Avatar] Offline
#3
Re: Transactions Need More Detail
Stefano, thanks for the reply.

This is how I understand things; please correct what I have wrong:

- every call to SaveChanges is it's own transaction that begins and ends

- if you need to do queries and database saves and have all of it to be in the same transaction, you need to use TransactionScope

- when I wrote a query like described here, http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/08/27/9665.aspx, doing a query and saves against the same database within one transaction, the error messages that results indicated that it was using MSDTC.

John
johnicello (8) [Avatar] Offline
#4
Re: Transactions Need More Detail
This thread helped me with this issue: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/58a2458f-9404-4686-b711-5cff8fb02181

To avoid 'MSDTC escalation' I needed to call ctx.Connection.Open(); When I put that statement in the method below, the transaction saved without error.

Here is a code sample for the helper method:

public class ContextHelper
{
public delegate void ContextHandler<T>(T ctx) where T : ObjectContext;

public static void Complete<T>(ContextHandler<T> handler, MyIsolationLevel isolationLevel = MyIsolationLevel.Serializable) where T: ObjectContext, new()
{
using (var ctx = new T())
{
var options = new TransactionOptions();
options.IsolationLevel = isolationLevel;

using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, options))
{
ctx.Connection.Open(); //****This statement was needed to avoid**** 'MSDTC escalation'
handler(ctx);

ctx.SaveChanges();
scope.Complete();
}
}
}

}
stefano.mostarda (47) [Avatar] Offline
#5
Re: Transactions Need More Detail
Hi John,

the open statement forces the use of a connection so the context always use that, I'll explain it better in the book.

Thanks for your GREAT feedback.

Byez
Stefano