LINQ Updates

LINQ maintains references to all of its relationships.  For instance, any PK or FK relationships are maintained as an object reference, so you can drill down into the PK or FK object or collection.  However, whenever you insert a new record, this reference is not populated (especially for PK's; haven't tested FK collections) whenever you call InsertOnSubmit, or add it to an entity set.  This reference is only populated whenever you submit the changes and the object is refreshed.

In a web application, it's not always best to submit changes after every insert, but for inserting new records, you have to do so to get the new key values and all.  For updates though, what you may want to consider is instead of assigning the key value, assign the reference value.  Let's say we have an employee class.  Employee references manager, a single entityref reference.  If the edit involves changing the manager, you can assign the managerkey, or the manager reference which is the object reference.  Instead of assigning the key value, you could assign the manager reference, and this updates the object to be the most up-to-date (manager key is updated with this approach too) and you don't have to call submitchanges to get this new reference.

 

Published Thursday, March 27, 2008 8:45 PM by bmains
Filed under:

Comments

# re: LINQ Updates

Saturday, March 29, 2008 8:29 PM by JoshC

Or cannot we get the latest value of the key from the table by comparing the values collection before and after the insert.

Josh Coswell

http://riverasp.net

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.