In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Transaction example Analysis of LINQ To SQL". In daily operation, I believe many people have doubts about LINQ To SQL Transaction example analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "LINQ To SQL Transaction example Analysis". Next, please follow the editor to study!
Whether you know from my book, or from MSDN or the website, the DataContext of LINQ to SQL automatically launches a Transaction even if you don't specify Transaction,DataContext when the SubmitChanges function is executed, which is quite a common design in many ORM.
But what if I don't want this default LINQ To SQL Transaction? There are many reasons, it may be to reduce the time of Lock, or efficiency, resources, etc., anyway, I just don't want this preset behavior! Just simply update the information.
Is it possible? As far as the current LINQ To SQL design is concerned, this behavior is mandatory and there is no room for adjustment, but! If you want to cross Guanshan is not without means of transportation, DataContext opened a small opening, so that we can override the SubmitChanges function, as a starting point, I used a lot of Reflection skills to make Transaction disappear.
Using System; using System.Data; using System.Data.Common; using System.Data.SqlClient; using System.Collections.Generic; using System.Linq; using System.Data.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication35 {class Program {static void Main (string [] args) {NorthwindDataContext context = new NorthwindDataContext (); var item = (from S1 in context.Customers where s1.CustomerID = = "VINET" select S1) .FirstOrDefault () If (item! = null) item.ContactName = "VINET14"; Console.ReadLine (); context.DisableTransaction = true; context.SubmitChanges (); Console.ReadLine ();}} partial class NorthwindDataContext {public bool DisableTransaction {get; set;} private static MethodInfo _ checkDispose = null; private static MethodInfo _ checkNotInSubmitChanges = null; private static MethodInfo _ verifyTrackingEnabled = null; private static MethodInfo _ acceptChanges = null; private static MethodInfo _ submitChanges = null; private static FieldInfo _ conflicts = null; private static FieldInfo _ isInSubmitChanges = null Private static PropertyInfo _ services = null; private static Type _ changeProcessorType = null; private static ConstructorInfo _ ci = null; static NorthwindDataContext () {_ checkDispose = typeof (DataContext) .GetMethod ("CheckDispose", BindingFlags.NonPublic | BindingFlags.Instance); _ checkNotInSubmitChanges = typeof (DataContext) .GetMethod ("CheckNotInSubmitChanges", BindingFlags.NonPublic | BindingFlags.Instance); _ verifyTrackingEnabled = typeof (DataContext) .GetMethod ("VerifyTrackingEnabled", BindingFlags.NonPublic | BindingFlags.Instance) _ acceptChanges = typeof (DataContext) .GetMethod ("AcceptChanges", BindingFlags.NonPublic | BindingFlags.Instance); _ conflicts = typeof (DataContext) .GetField ("conflicts", BindingFlags.NonPublic | BindingFlags.Instance); _ isInSubmitChanges = typeof (DataContext) .GetField ("isInSubmitChanges", BindingFlags.NonPublic | BindingFlags.Instance); _ changeProcessorType = typeof (DataContext). Assembly.GetType ("System.Data.Linq.ChangeProcessor"); _ services = typeof (DataContext) .GetProperty ("Services", BindingFlags.NonPublic | BindingFlags.Instance) _ ci = _ changeProcessorType.GetConstructor (BindingFlags.NonPublic | BindingFlags.Instance, null, new Type [] {typeof (DataContext) .Assembly.GetType ("System.Data.Linq.CommonDataServices"), typeof (DataContext)}, null); _ submitChanges = _ changeProcessorType.GetMethod ("SubmitChanges", BindingFlags.NonPublic | BindingFlags.Instance);} public override void SubmitChanges (System.Data.Linq.ConflictMode failureMode) {if (DisableTransaction) {_ checkDispose.Invoke (this, null); _ checkNotInSubmitChanges.Invoke (this, null) _ verifyTrackingEnabled.Invoke (this, null); ((ChangeConflictCollection) _ conflicts.GetValue (this)). Clear (); try {_ isInSubmitChanges.SetValue (this, true); object processor = _ ci.Invoke (new object [] {_ services.GetValue (this, null), this}); _ submitChanges.Invoke (processor, new object [] {failureMode}); _ acceptChanges.Invoke (this, null);} finally {_ isInSubmitChanges.SetValue (this, false) } else base.SubmitChanges (failureMode);}
After processing, I personally feel that LINQ To SQL Transaction should be treated as a Session concept, such as Hibernate.
At this point, the study of "Transaction example Analysis of LINQ To SQL" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 208
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.