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