views:

229

answers:

3

I'm trying to determine if any changes were made to a particular entity object. Essentially, I want to know if SubmitChanges() will actually change anything. I would prefer to be able to determine this after SubmitChanges() has been called, but it doesn't really matter.

Anyone know how I would do this?

+5  A: 

Take a look at the GetChangeset function on your DataContext.

joshperry
But watch out for the side effects mentioned in the linked MSDN article
Paul Nearney
Is there any way to see the changes for a particular object? There will likely be changes that I don't care about.
Jeremy Cantrell
+1  A: 

This is what I came up with:

Public Function HasChanges(ByVal obj As Object) As Boolean
    Dim cs = GetChangeSet()
    If cs.Updates.Contains(obj) Or cs.Inserts.Contains(obj) Or cs.Deletes.Contains(obj) Then Return True
    Return False
End Function
Jeremy Cantrell
+2  A: 

You can directly access the changes for an attached entity instance using the parent table:

var entityType = typeof(myEntity);
var table = dataContext.GetTable<entityType>();
var modifiedMembers = table.GetModifiedMembers(myEntity);

if (modifiedMembers.Any())
{
      ... changes were made
}
else
{
      ... no changes were made
}

BUT - you have to do it before SubmitChanges(), of course. I use this approach instead of GetChangeSet(), due to the better type fidelity, and the fact that you can easily inspect the changes themselves.