Introducing LINQ to SQL Designer using Visual Studio 2008 - Explanation of handling transactions in LINQ to SQL
(Page 5 of 5 )
The "LINQ to SQL" framework automatically encloses the modifications into a transaction, if it is trying to handle more than one row at a time (say, submitting more rows to save to a database).
Let us try to understand each line from the previous section.
Dim db As New SampleDBDataContext
Dim depts As Table(Of dept) = db.depts
The above creates an instance to the database context. Through the context object, we interact with the database. In other words, it is the main interface. As "Department" is the parent table, I created a reference to the collection of "dept" models. This can be used to add "dept" objects directly to it. We can also do the same using "db.depts" directly.
Dim NewDept As New dept
NewDept.deptno = 50
NewDept.dname = "Legal"
Adding a new department involves creating a new "dept" object. The above does the same. It instantiates a "dept" object and provides the values for the properties.
'create new emp
Dim NewEmp1 As New emp
NewEmp1.empno = 2001
NewEmp1.ename = "aaa"
NewEmp1.sal = "2500"
NewEmp1.deptno = 50
'create another emp
Dim NewEmp2 As New emp
NewEmp2.empno = 2002
NewEmp2.ename = "bbb"
NewEmp2.sal = "5500"
NewEmp2.deptno = 50
Once the "Dept" object is ready, we need to add a few employees (say "emp" objects) to the newly created "dept" object. The above creates two new "emp" objects and provides the values for their properties.
NewDept.emps.Add(NewEmp1)
NewDept.emps.Add(NewEmp2)
As the two employees are related to the same department, we need to add those to the sub-collection of "Dept" object (which is nothing but the "Emps" collection). The above does the same.
db.depts.InsertOnSubmit(NewDept)
db.SubmitChanges()
The first line in the above sample adds the entire dept structure (along with employees) to the "depts" collection of the database context. Finally, we save it using the second line.
The "db.SubmitChanges()" opens a transaction. The transaction is implicitly handled as part of the "SubmitChanges" method.
Make sure that if the "SubmitChanges" method needs to work with the explicit transaction (say distributed), we can execute the statement in a separate "TransactionScope" enclosure as follows:
Using ts As New System.Transactions.TransactionScope
.
.
db.SubmitChanges()
.
.
End Using
I hope you enjoyed the article. Any suggestions, bugs, errors, enhancements, etc. are highly appreciated at http://jagchat.spaces.live.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |