0
votes

I have below method

public void UpdateQuantity()
    {
        Sql ss = new Sql();
        M3 m3 = new M3();

        TransactionOptions ff = new TransactionOptions();
        ff.IsolationLevel = IsolationLevel.ReadUncommitted;

        using (TransactionScope dd = new TransactionScope(TransactionScopeOption.Required, ff))
        {                
            try
            {                   
                ss.AddRegion("ALFKI", "SES1"); //step 1
                m3.UpdateAnotherSystem(); //step2
                dd.Complete();                   
            }
            catch (Exception)
            {

            }
        }
    }

public void AddRegion(string customerName, string Deception)
    {
        using (NorthWind context = new NorthWind())
        {

            Region rr = new Region();
            rr.RegionID = 5;
            rr.RegionDescription = "Ssaman";
            context.Regions.Add(rr);
            try
            {

                context.SaveChanges();
            }

            catch (Exception)
            {
                throw;
            }
        }
    }

In that first im going to update Sql server data base .After that im going to perform another update on other system.If step2 fails(may be network failure) then i need to reverse step 1.There for i put two method calls inside the transactionscope. I'm use entity framework to work with sql.Entity framework always set the transaction isolation level as read committed(according to the sql profiler).

but my problem is after context.SaveChanges() called my target table is locked till transaction completes(dd.Complete()).

Are there are any way to change entity framework transaction isolation level?(My entity framework version is 5).

2
In your example you used ReadUncommitted, just specify another value - ken2k
@ken2k: It did not reflect. - Renushi

2 Answers

0
votes

SQL Server does not release locks that were taken due to writes until the end of the transaction. This is so that writes can be rolled back. You cannot do anything about this.

End your transaction or live with the fact that the rows written are still in use. Normally, this is not a problem. You should probably have a single context, connection and transaction for most work that happens in an HTTP request or WCF request. Transactions do not block on themselves.

0
votes
using (var context = new BloggingContext()) 
        { 
            using (var dbContextTransaction = context.Database.BeginTransaction()) 
            { 
                try 
                { 
                    context.Database.ExecuteSqlCommand( 
                        @"UPDATE Blogs SET Rating = 5" + 
                            " WHERE Name LIKE '%Entity Framework%'" 
                        ); 

                    var query = context.Posts.Where(p => p.Blog.Rating >= 5); 
                    foreach (var post in query) 
                    { 
                        post.Title += "[Cool Blog]"; 
                    } 

                    context.SaveChanges(); 

                    dbContextTransaction.Commit(); 
                } 
                catch (Exception) 
                { 
                    dbContextTransaction.Rollback(); 
                } 
            } 
        }