none
Linq to SQL: How to determine which object properties are changed? RRS feed

  • Question

  • I need to program some CRUD data validation methods for Linq to SQL objects before the object is updated by dataContext.submitChanges(). 

    I thought Linq might maintain a user-accessible collection of object properties for those properties which have changed. 

    Or do I have to go the brute force expensive route and iterate over all of the properties to find those that have been changed?

    Thanks,  -BGood
    Thursday, January 14, 2010 5:44 PM

All replies

  • Take a look at the DataContext.GetChangeSet method . You can use it to access the following ChangeSet properties : Inserts, Updates, and Deletes.

    Bear in mind that these are tracked changes before an actual submission occurs, and therefore don't accurately reflect what may occur (ie. an action could fail once submitted).
    Document my code? Why do you think it's called "code"?
    • Proposed as answer by LitEnders Wednesday, September 22, 2010 8:51 PM
    Thursday, January 14, 2010 6:11 PM
  • Thanks for the reference to the .getchangeset() method, Ahmad.  In my dbml.vb class, I have wired up the OnValidate event in order to explore which properties of my SC object have changed.  Although me. refers to the SC object and backing properties, I don't see that my datacontext is necessarily in scope, and cannot simply "dim x as changeset" in my OnValidate method as is illustrated in the msdn examples. 

    Partial Class SC
        Private Sub OnValidate(ByVal action As System.Data.Linq.ChangeAction)
            MsgBox("SC validation: " & action.ToString)
        End Sub
    End Class

    Is there an imports I am missing and how do I find the collection of changed properties?

    Thanks, -BGood

    Monday, January 18, 2010 3:25 PM
  • Just as a follow-up to my earlier post, for whatever reason some of the Linq methods and properties did not show up in intellisense, but were in fact accepted by the compiler and .GetChangeSet was indeed very helpful in object validation.  Specifically, the modified member info provided by:

    Dim mmi As System.Data.Linq.ModifiedMemberInfo() _
                    = myForm.db.GetTable(Of myObject).GetModifiedMembers(Me)

    Unfortunately, when corrections are made to the databound textboxes, text changes only show up in mmi if the text is completely deleted (rather than simply edited).

    Thanks for your help.

                                -BGood
    Saturday, January 30, 2010 5:52 PM