您好,登錄后才能下訂單哦!
這篇文章主要介紹“LINQ To SQL的Transaction舉例分析”,在日常操作中,相信很多人在LINQ To SQL的Transaction舉例分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”LINQ To SQL的Transaction舉例分析”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
不管你是由我的書中,或是MSDN、網(wǎng)站處得知,LINQ to SQL之DataContext于SubmitChanges函式執(zhí)行時(shí),就算不指定Transaction,DataContext都會(huì)自動(dòng)啟動(dòng)一個(gè) Transaction,在許多ORM中,這算是相當(dāng)常見的設(shè)計(jì)。
不過,如果我不想要這個(gè)預(yù)設(shè)的LINQ To SQL Transaction呢?原因有很多,可能是為了減少Lock的時(shí)間,或是效能、資源等等,反正就是不想要這個(gè)預(yù)設(shè)行為就是了!只要簡簡單單的更新資料就好了。
可能嗎?就目前的LINQ To SQL設(shè)計(jì)來說,這個(gè)行為是強(qiáng)制性的,且無可調(diào)整之空間,但!若要強(qiáng)渡關(guān)山也不是沒交通工具,DataContext開了一個(gè)小口,讓我們可以覆載 SubmitChanges函式,以此為起點(diǎn),我利用了大量的Reflection技巧,讓Transaction消失。
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); } } }
處理完畢,我個(gè)人是覺得,應(yīng)該把LINQ To SQL Transaction以Session概念處理,如Hibernate。
到此,關(guān)于“LINQ To SQL的Transaction舉例分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。