本文向大家介绍LINQ To SQL Transaction,可能好多人还不了解Transaction,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。
创新互联主要从事网站建设、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务隆林,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
不管你是由我的书中,或是MSDN、网站处得知,LINQ to SQL之DataContext于SubmitChanges函式执行时,就算不指定Transaction,DataContext都会自动启动一个 Transaction,在许多ORM中,这算是相当常见的设计。
不过,如果我不想要这个预设的LINQ To SQL Transaction呢?原因有很多,可能是为了减少Lock的时间,或是效能、资源等等,反正就是不想要这个预设行为就是了!只要简简单单的更新资料就好了。
可能吗?就目前的LINQ To SQL设计来说,这个行为是强制性的,且无可调整之空间,但!若要强渡关山也不是没交通工具,DataContext开了一个小口,让我们可以覆载 SubmitChanges函式,以此为起点,我利用了大量的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);
- }
- }
- }
处理完毕,我个人是觉得,应该把LINQ To SQL Transaction以Session概念处理,如Hibernate。
新闻标题:LINQToSQLTransaction浅析
当前路径:http://www.shufengxianlan.com/qtweb/news46/265346.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联