Linq to SQL DataContext



Log
                StreamWriter sw =new StreamWriter(Path.Combine(Directory.GetCurrentDirectory(),"log.txt"),true);
                sw = newStreamWriter(Path.Combine(Directory.GetCurrentDirectory(),"log.txt"), true);
                this.Log = sw;


Attach external object to DataContext
                Customer c = newCustomer { CustomerID = "ALFKI",
                ContactName ="zhuye", CompanyName = "1111" };
                ctx.Customers.Attach(c,true);
                ctx.SubmitChanges();


Defer Loading at property level


                -but whencontext.DeferredLoadingEnabled = false or context.ObjectTrackingEnabled = falsethen delay loading will fail
                -Explict Load:
                                DataLoadOptions options = new DataLoadOptions();
                                options.LoadWith<Product>(p =>p.Order_Details);
                                ctx.LoadOptions = options;
                                var products = from p in ctx.Products select p;
                               
Change Tracking and DataContext.SubmitChanges()
                -LINQ to SQL changetracking happens on the consuming caller side - and not in the database. Thismeans that you are not consuming any database resources when using it, nor doyou need to change/install anything in the database to enable it.After makingthe changes we want to the objects we've retrieved from LINQ to SQL, we canthen optionally call the "SubmitChanges()" method on our DataContextto apply the changes back to the database.

-One of the cool things about having our SPROC return "Product" objects like above is that LINQ to SQL will automatically track the changes made to the returned Product objects just like it would Product objects returned via LINQ queries.  When we call the "SubmitChanges()" method on our DataContext, the changes we have made to these objects will automatically be saved back to the database. (from http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx)
               


Confilct
                -var query = from pin ctx.Products where p.CategoryID == 1 select p;
                foreach (var p inquery)
                p.UnitsInStock =Convert.ToInt16(p.UnitsInStock - 1);
                try
                {
                                ctx.SubmitChanges(ConflictMode.ContinueOnConflict);
                }
                catch(ChangeConflictException)
                {
                                foreach (ObjectChangeConflict cc inctx.ChangeConflicts)
                                {
                                Product p = (Product)cc.Object;
                                Response.Write(p.ProductID +"<br/>");
                               
                                //Swap the original values with the valuesretrieved from the database. No current value is modified
            occ.Resolve(RefreshMode.KeepCurrentValues);
                                 // All database values overwrite current values with//values from database
           occ.Resolve(RefreshMode.OverwriteCurrentValues);
                                 //Keep current values that have changed, //updates other values with database values
                                                occ.Resolve(RefreshMode.KeepChanges);
                               
                                }
                }
                ctx.SubmitChanges();
               
Transaction

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值