LINQ to SQL (Part 4 - Updating our Database)
page 3 of 12
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 82371/ 106

Change Tracking and DataContext.SubmitChanges()

When we perform queries and retrieve objects like the product instances above, LINQ to SQL will by default keep track of any changes or updates we later make to these objects.  We can make any number of queries and changes we want using a LINQ to SQL DataContext, and these changes will all be tracked together. 

Note: LINQ to SQL change tracking happens on the consuming caller side - and not in the database.  This means that you are not consuming any database resources when using it, nor do you need to change/install anything in the database to enable it.

After making the changes we want to the objects we've retrieved from LINQ to SQL, we can then optionally call the "SubmitChanges()" method on our DataContext to apply the changes back to the database.  This will cause LINQ to SQL to dynamically calculate and execute the appropriate SQL code to update the database.

For example, I could write the below code to update the price and # of units in stock of the "Chai" product in the database:

Figure 4

When I call northwind.SubmitChanges() above, LINQ to SQL will dynamically construct and execute a SQL "UPDATE" statement that will update the two product property values we modified above.

I could then write the below code to loop over unpopular, expensive products and set the "ReorderLevel" property of them to zero:

Figure 5

When I call northwind.SubmitChanges() above, LINQ to SQL will calculate and execute an appropriate set of UPDATE statements to modify the products who had their ReorderLevel property changed.

Note that if a Product's property values weren't changed by the property assignments above, then the object would not be considered changed and LINQ to SQL would therefore not execute an update for that product back to the database.  For example - if the "Chai" product's unitprice was already $2 and the number of units in stock was 4, then calling SubmitChanges() would not cause any database update statements to execute.  Likewise, only those products in the second example whose ReorderLevel was not already 0 would be updated when the SubmitChanges() method was called.


View Entire Article

User Comments

Title: Good Article   
Name: Vishnu Shanmugam
Date: 2010-12-11 12:46:40 AM
Comment:
Excellent article.
Title: very clear   
Name: tarun
Date: 2010-08-13 7:31:17 AM
Comment:
thanks for the clear explanation
Title: this is terrible   
Name: mac
Date: 2010-08-02 4:56:22 PM
Comment:
this is by far the worst display of CRUD i have seen yet.
Title: How can I insert dates   
Name: Ernie Smart
Date: 2010-03-17 1:08:05 PM
Comment:
How can I parse or convert dates? Let's say I have a text box (txtDate) to enter the dates and the database has a field called OrderDate. If either of the follwoing two:
OrderDate = Convert.ToDateTime(txtDate)
OrderDate = DateTime.Parse(txtDate)
I get System.FormatException: String was not recognized as a valid DateTime error.
If I do OrderDate = DateTime.Now, the code executes correctly.
How can convert the date entered in the text box so the program will work?
Thanks
Title: i can't find add,update or delete methods for my data context.   
Name: Enow Mbi
Date: 2010-02-19 11:10:15 AM
Comment:
GREAT ARTICLE.
I can't figure out why i can't find,add,update or delete methods of my data context despite the fact that my table has a primary key.
Title: More Article   
Name: twinsf
Date: 2010-01-28 2:09:59 AM
Comment:
thank youu for helpfull
Title: Bad advice   
Name: Simon
Date: 2008-12-28 11:59:06 PM
Comment:
The partial class validation logic you demonstrate is not testable and is therefore useless to me. Isn't it better advice to put the validation logic in business objects (as Rocky Lhotka would urge). Or alternatively, to put it in a service layer or repository that is testable. Baking your validation logic into these proprietary, tightly coupled, LINQ DTOs seems wrong.
Title: Create items together   
Name: Tom
Date: 2008-12-24 1:49:56 PM
Comment:
Is there a way to create a category and product together? From what I've seen, beverages.Products would be null, so you can't call "Add" on it. You also can't set Products equal to a new EntitySet, because the setter assumes a non-null EntitySet already which it calls "Assign" on.

Thanks!
Title: Very helpful   
Name: Alexander
Date: 2008-11-13 9:20:30 PM
Comment:
Just great. Killed a few days with msdn stuff trying to comprehend logic of Entity Insert/Update/Delete customisatiton. Just got it from this article in a couple of minutes.
Thank you.
Title: Linq doesn't update   
Name: Moez Tounsi
Date: 2008-10-07 12:15:53 PM
Comment:
Following the example above, ling will add a new record with the new parameters.
We have to handle the deletion of the old element.
Title: Excellent   
Name: Wayne
Date: 2008-07-23 12:27:59 AM
Comment:
Excellent article, Well done, a really good read.
Title: Thanks a lot   
Name: Andi
Date: 2007-12-02 11:25:10 AM
Comment:
Thank you for this great article!!!






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-09-21 7:55:19 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search