Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the LINQ to SQL transaction models?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what the LINQ to SQL transaction model has, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand.

Three LINQ to SQL transaction models are:

◆ explicit local transaction: when SubmitChanges is called, if the Transaction property is set to transaction, the SubmitChanges call is performed in the context of the same transaction. After the transaction is executed successfully, it is up to you to commit or roll back the transaction. The connection corresponding to the transaction must match the connection used to construct the DataContext. If a different connection is used, an exception is thrown.

◆ explicitly distributable transactions: LINQ to SQL API can be called within the scope of the current Transaction (including but not limited to SubmitChanges). LINQ to SQL detects that the call is within the scope of the transaction and does not create a new transaction. In this case, vbtecdlinq also avoids closing the connection. You can perform queries and SubmitChanges operations in the context of such transactions.

◆ implicit transaction: when you call SubmitChanges, LINQ to SQL checks to see if the call is within the scope of Transaction or whether the Transaction property is set to a user-initiated local transaction. If it does not find either transaction, LINQ to SQL starts the local transaction and uses it to execute the generated SQL command. When all SQL commands have been successfully executed, LINQ to SQL commits the local transaction and returns.

1.Implicit (implicit)

Note: this example implicitly uses transactions when performing the SubmitChanges () operation. Because when updating the inventory quantity of two products, the inventory quantity of the second product is negative, which violates the CHECK constraint on the server. This causes all updates to fail and the system rolls back to the initial state of the operation.

Try {Product prod1 = db.Products.First (p = > p.ProductID = = 4); Product prod2 = db.Products.First (p = > p.ProductID = = 5); prod1.UnitsInStock-= 3; prod2.UnitsInStock-= 5 db.SubmitChanges / error: units of inventory quantity cannot be negative / / either all succeed or all fail db.SubmitChanges ();} catch (System.Data.SqlClient.SqlException e) {/ / perform exception handling}

2.Explicit (explicit)

Description: this example uses explicit transactions. Explicit transactions can provide more protection by adding data reads to transactions to prevent open concurrency exceptions. As in the previous query, updating the UnitsInStock field of prod2 will make the field negative, which violates the CHECK constraint in the database. This causes the transaction to update both products to fail, and all changes will be rolled back. The LINQ to SQL transaction model is introduced above.

Using (TransactionScope ts = new TransactionScope ()) {try {Product prod1 = db.Products.First (p = > p.ProductID = = 4); Product prod2 = db.Products.First (p = > p.ProductID = = 5); prod1.UnitsInStock-= 3; prod2.UnitsInStock-= 5 p.ProductID / error: the unit of inventory quantity cannot be negative db.SubmitChanges () } catch (System.Data.SqlClient.SqlException e) {/ / execute exception handling}} Thank you for reading this article carefully. I hope the article "what are the LINQ to SQL transaction models shared by the editor will be helpful to you?" at the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report