成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

LINQ To SQL Transaction淺析

開發 后端
這里介紹LINQ To SQL Transaction,如果我不想要這個預設的LINQ To SQL Transaction呢?原因有很多,可能是為了減少Lock的時間,或是效能、資源等等,反正就是不想要這個預設行為就是了!只要簡簡單單的更新資料就好了。

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

不管你是由我的書中,或是MSDN、網站處得知,LINQ to SQL之DataContext于SubmitChanges函式執行時,就算不指定Transaction,DataContext都會自動啟動一個 Transaction,在許多ORM中,這算是相當常見的設計。

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

可能嗎?就目前的LINQ To SQL設計來說,這個行為是強制性的,且無可調整之空間,但!若要強渡關山也不是沒交通工具,DataContext開了一個小口,讓我們可以覆載 SubmitChanges函式,以此為起點,我利用了大量的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. }  

處理完畢,我個人是覺得,應該把LINQ To SQL Transaction以Session概念處理,如Hibernate。

【編輯推薦】

  1. LINQ Customers類概括
  2. LINQ查詢操作全面分析
  3. LINQ to SQL的Table剖析
  4. Linq數據分組全面描述
  5. LINQ查詢基礎概括
責任編輯:佚名 來源: IT168
相關推薦

2009-09-10 18:02:23

LINQ to SQL

2009-09-17 17:34:23

linq to sql

2009-09-15 10:12:37

LINQ To SQL

2009-09-14 09:46:00

LINQ to SQL

2009-09-17 18:05:15

linq to sql

2009-09-10 10:09:46

LINQ to SQL

2009-09-14 17:40:47

LINQ To SQL

2009-09-18 14:25:36

LINQ to SQL

2009-09-15 14:30:11

Linq連接

2009-09-17 13:30:32

LINQ to XML

2009-09-14 16:46:15

LINQ to XML

2009-09-07 16:44:28

Linq String

2009-09-16 15:33:22

LINQ to XML

2009-09-15 13:30:54

linq級聯

2009-09-14 19:20:22

LINQ TO SQL

2009-09-10 14:47:53

Linq .NET查詢

2009-09-14 13:37:25

LINQ ADO.NE

2009-09-17 10:57:06

Linq隨機讀取數據

2009-09-15 09:19:22

linq動態條件

2009-09-14 18:23:59

LINQ嵌套查詢
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美一区二区在线观看 | 国产99视频精品免视看9 | 久久精品福利视频 | 亚洲福利精品 | 狠狠骚| 亚洲 欧美 另类 日韩 | 久久国内精品 | 欧美一二区 | 国产伦精品一区二区三区高清 | 国产精品久久久久久久久久久新郎 | 欧美日韩一区二区三区四区 | 精品自拍视频在线观看 | 青青草原综合久久大伊人精品 | 99色视频| 女同久久另类99精品国产 | 精精国产xxxx视频在线播放 | 日本精品久久 | 欧美日韩国产一区二区三区 | 精品视频在线免费观看 | 久久久一区二区三区四区 | 91精品国产美女在线观看 | 精品av久久久久电影 | 亚洲激情在线观看 | 久久久人成影片一区二区三区 | 精品久久久久久久 | a成人| 日韩亚洲一区二区 | 精品美女在线观看 | 91人人看| 欧美精品一区二区免费 | 亚洲视频在线观看一区二区三区 | 91天堂| 天天躁日日躁狠狠躁白人 | 天天操天天舔 | 在线免费观看成人 | 欧美男人天堂 | 精品国产成人 | 亚洲午夜在线 | 日日夜夜狠狠操 | 超碰在线影院 | 麻豆一区一区三区四区 |