Using transactions with LINQ-to-SQL

Today one of my colleague asked that how we can use transactions with the LINQ-to-SQL Classes when we use more then one entities updated at same time. It was a good question. Here is my answer for that.For ASP.NET 2.0  or higher version have a new class called TransactionScope which can be used to manage transaction with the LINQ.

Let’s take a simple scenario we are having a shopping cart application in which we are storing details or particular order placed into the database using LINQ-to-SQL. There are two tables Order and OrderDetails which will have all the information related to order. Order will store particular information about orders while OrderDetails table will have product and quantity of product for particular order.We need to insert data in both tables as same time and if any errors comes then it should rollback the transaction.

To use TransactionScope in above scenario first we have add a reference to System.Transactions like below.

TransactionScope,System.Transactions

After adding the transaction we need to drag and drop the Order and Order Details tables into Linq-To-SQL Classes it will create entities for that. Below is the code for transaction scope to use mange transaction with Linq Context.

 MyContextDataContext objContext = new MyContextDataContext();
using (System.Transactions.TransactionScope tScope
= new System.Transactions.TransactionScope(TransactionScopeOption.Required))
{
objContext.Order.InsertOnSubmit(Order);
objContext.OrderDetails.InsertOnSumbit(OrderDetails);
objContext.SubmitChanges();
tScope.Complete();
}

Here it will commit transaction only if using blocks will run successfully. Hope this will help you.

Shout it
Published Thursday, May 20, 2010 2:34 AM by Jalpesh P. Vadgama

Comments

# re: Using transactions with LINQ-to-SQL

Wednesday, May 19, 2010 6:08 PM by zoldello

Thanks for the handy information.

# Please tell me about the Top MS web hosting for my website.?

Pingback from  Please tell me about the Top MS web hosting for my website.?

# Using transactions with LINQ-to-SQL – DotNetJaps - sql

Pingback from  Using transactions with LINQ-to-SQL – DotNetJaps - sql

# Is it possible to buy and sell on ebay without using paypal or similar transaction systems?

Pingback from  Is it possible to buy and sell on ebay without using paypal or similar transaction systems?

# Twitter Trackbacks for Using transactions with LINQ-to-SQL - DotNetJaps [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Using transactions with LINQ-to-SQL - DotNetJaps         [asp.net]        on Topsy.com

# Scripting SQL Objects with SQL 2008

Thursday, May 20, 2010 3:58 AM by Scripting SQL Objects with SQL 2008

Pingback from  Scripting SQL Objects with SQL 2008

# re: Using transactions with LINQ-to-SQL

Thursday, May 20, 2010 9:08 AM by Webdiyer

Why not attach OrderDetails as a property of Order and insert only order? linq to sql will insert both order and order details and automatically wrap them into a single transaction. eg.:

Order myorder=new Order();

myorder.OrderDate=...;

...

foreach(OrderDetail od in orderDetailsList){

Order.OrderDetails.Add(od);

}

objContext.Order.InsertOnSubmit(Order);

objContext.SubmitChanges();

# re: Using transactions with LINQ-to-SQL

Thursday, May 20, 2010 11:05 AM by Jalpesh P. Vadgama

Yes you can do that way also. But there are some scenario where you have lots of tables more then 10 to 12 tables then in this kind of things. It will be performance loss that's why this will be beteter

# What’s the benefit of having a dedicated/virtual hosting plan when using asp.net?

Pingback from  What’s the benefit of having a dedicated/virtual hosting plan when using asp.net?

# davidrenz.com » Using transactions with LINQ-to-SQL

Thursday, July 22, 2010 12:33 PM by davidrenz.com » Using transactions with LINQ-to-SQL

Pingback from  davidrenz.com   » Using transactions with LINQ-to-SQL

Leave a Comment

(required) 
(required) 
(optional)
(required)