日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
LINQToSQLTransaction淺析

本文向大家介紹LINQ To SQL Transaction,可能好多人還不了解Transaction,沒(méi)有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。

創(chuàng)新互聯(lián)主要從事網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)隆林,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792

不管你是由我的書(shū)中,或是MSDN、網(wǎng)站處得知,LINQ to SQL之DataContext于SubmitChanges函式執(zhí)行時(shí),就算不指定Transaction,DataContext都會(huì)自動(dòng)啟動(dòng)一個(gè) Transaction,在許多ORM中,這算是相當(dāng)常見(jiàn)的設(shè)計(jì)。

不過(guò),如果我不想要這個(gè)預(yù)設(shè)的LINQ To SQL Transaction呢?原因有很多,可能是為了減少Lock的時(shí)間,或是效能、資源等等,反正就是不想要這個(gè)預(yù)設(shè)行為就是了!只要簡(jiǎn)簡(jiǎn)單單的更新資料就好了。

可能嗎?就目前的LINQ To SQL設(shè)計(jì)來(lái)說(shuō),這個(gè)行為是強(qiáng)制性的,且無(wú)可調(diào)整之空間,但!若要強(qiáng)渡關(guān)山也不是沒(méi)交通工具,DataContext開(kāi)了一個(gè)小口,讓我們可以覆載 SubmitChanges函式,以此為起點(diǎn),我利用了大量的Reflection技巧,讓Transaction消失。

 
 
 
  1. using System;  
  2. using System.Data;  
  3. using System.Data.Common;  
  4. using System.Data.SqlClient;  
  5. using System.Collections.Generic;  
  6. using System.Linq;  
  7. using System.Data.Linq;  
  8. using System.Text;  
  9. using System.Reflection;  
  10.    
  11. namespace ConsoleApplication35  
  12. {  
  13. class Program  
  14. {  
  15. static void Main(string[] args)  
  16. {  
  17. NorthwindDataContext context = new NorthwindDataContext();   
  18. var item = (from s1 in context.Customers where s1.CustomerID == "VINET"  
  19. select s1).FirstOrDefault();  
  20. if (item != null)  
  21. item.ContactName = "VINET14";  
  22. Console.ReadLine();  
  23. context.DisableTransaction = true;  
  24. context.SubmitChanges();  
  25. Console.ReadLine();  
  26. }  
  27. }  
  28.    
  29. partial class NorthwindDataContext  
  30. {  
  31. public bool DisableTransaction { get; set; }  
  32.    
  33. private static MethodInfo _checkDispose = null;  
  34. private static MethodInfo _checkNotInSubmitChanges = null;   
  35. private static MethodInfo _verifyTrackingEnabled = null;  
  36. private static MethodInfo _acceptChanges = null;  
  37. private static MethodInfo _submitChanges = null;  
  38. private static FieldInfo _conflicts = null;  
  39. private static FieldInfo _isInSubmitChanges = null;  
  40. private static PropertyInfo _services = null;  
  41. private static Type _changeProcessorType = null;  
  42. private static ConstructorInfo _ci = null;  
  43.    
  44. static NorthwindDataContext()  
  45. {  
  46. _checkDispose = typeof(DataContext).GetMethod("CheckDispose",  
  47.  BindingFlags.NonPublic | BindingFlags.Instance);  
  48. _checkNotInSubmitChanges =  
  49.  typeof(DataContext).GetMethod("CheckNotInSubmitChanges",  
  50. BindingFlags.NonPublic | BindingFlags.Instance);  
  51. _verifyTrackingEnabled = typeof(DataContext).GetMethod("VerifyTrackingEnabled",  
  52. BindingFlags.NonPublic | BindingFlags.Instance);  
  53. _acceptChanges = typeof(DataContext).GetMethod("AcceptChanges",  
  54.  BindingFlags.NonPublic | BindingFlags.Instance);  
  55. _conflicts = typeof(DataContext).GetField("conflicts",  
  56. BindingFlags.NonPublic | BindingFlags.Instance);  
  57. _isInSubmitChanges = typeof(DataContext).GetField("isInSubmitChanges",  
  58.  BindingFlags.NonPublic | BindingFlags.Instance);  
  59. _changeProcessorType = typeof(DataContext).Assembly.GetType(  
  60.  "System.Data.Linq.ChangeProcessor");  
  61. _services = typeof(DataContext).GetProperty("Services",  
  62. BindingFlags.NonPublic | BindingFlags.Instance);  
  63. _ci = _changeProcessorType.GetConstructor(  
  64.  BindingFlags.NonPublic | BindingFlags.Instance, null,  
  65. new Type[]  
  66. { typeof(DataContext).Assembly.GetType("System.Data.Linq.CommonDataServices"),  
  67. typeof(DataContext) }, null);  
  68. _submitChanges = _changeProcessorType.GetMethod("SubmitChanges",  
  69.  BindingFlags.NonPublic | BindingFlags.Instance);  
  70. }  
  71.    
  72. public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)   
  73. {  
  74. if (DisableTransaction)  
  75. {  
  76. _checkDispose.Invoke(this, null);  
  77. _checkNotInSubmitChanges.Invoke(this, null);  
  78. _verifyTrackingEnabled.Invoke(this, null);  
  79. ((ChangeConflictCollection)_conflicts.GetValue(this)).Clear();  
  80. try  
  81. {  
  82. _isInSubmitChanges.SetValue(this, true);  
  83. object processor = _ci.Invoke(new object[]  
  84.  { _services.GetValue(this, null), this });  
  85. _submitChanges.Invoke(processor, new object[] { failureMode });  
  86. _acceptChanges.Invoke(this, null);  
  87. }  
  88. finally  
  89. {  
  90. _isInSubmitChanges.SetValue(this, false);  
  91. }  
  92. }  
  93. else  
  94. base.SubmitChanges(failureMode);  
  95. }  
  96. }  

處理完畢,我個(gè)人是覺(jué)得,應(yīng)該把LINQ To SQL Transaction以Session概念處理,如Hibernate。


新聞名稱:LINQToSQLTransaction淺析
URL地址:http://www.5511xx.com/article/dpoeich.html