views:

369

answers:

1

I'm trying to extend the linqtosql classes generated by the VS designer and need to determine if the value of a specific field has changed. Is there a way for me to access the before and after values for a field in the DataContext Update method for a table/entity?

Here's my code:

public partial class DataClassesDataContext
{
    partial void UpdateActivity(Activity instance)
    {
        this.ExecuteDynamicUpdate(instance);
        //need to compare before and after values to determine if instance.AssignedTo value has changed and take action if it has
    }
}

I'm also open to adding a property to the Activity entity class to signal whether the value has changed but I can't figure out how to tell if the value has changed there either. I can't just use the OnAssignedToChanged method of the Activity class because it fires whenever the property value is set, not necessarily changed. I'm using the ListView and LINQDataSource control for updating so it gets set no matter what.

I also thought I might be able to use the OnAssignedToChanging method but the Activity instance does not seem to have current values at that point. The following code does not work as this.AssignedTo is always null.

partial void OnAssignedToChanging(int? value)
{
   if (value != this.AssignedTo)
   {
      _reassigned = true;
   }
}
+4  A: 

You should be able to do this:

public partial class DataClassesDataContext
{
    partial void UpdateActivity(Activity instance)
    {
        Activity originalActivity = Activities.GetOriginalEntityState(instance);
        if (instance.Property != originalActivity.Property)
        {
            // Do stuff
        }
        this.ExecuteDynamicUpdate(instance);
        //need to compare before and after values to determine if instance.AssignedTo value has changed and take action if it has
    }
}

Another alternative:

public partial class DataClassesDataContext
{
    partial void UpdateActivity(Activity instance)
    {
        ModifiedMemberInfo[] changes = Activities.GetModifiedMembers(instance);
        foreach (var change in changes)
        {
            Console.WriteLine("Member: {0}, Orig: {1}, New: {2}", change.Member, change.OriginalValue, change.CurrentValue);
        }

        this.ExecuteDynamicUpdate(instance);
        //need to compare before and after values to determine if instance.AssignedTo value has changed and take action if it has
    }
}

I just checked into your other option (OnAssignedToChanging(int? value)), and it seems to work fine for me. Are you sure the initial value wasn't actually null? I tested it with a new object as well as one pulled from a database and it appears to work correctly.

Ryan Versaw
partial method? ;)
Kezzer
Yeah, am I missing something? Partial methods were added in .NET 3.5 - look them up if you're interested :)
Ryan Versaw
Basically, a partial class can have partial methods declared, but not defined. You can then optionally implement them in a separate file. If you don't, it works as though all associated method calls didn't exist.
Ryan Versaw
Works great for me. Thanks Ryan!
joshb