I have a single linq to sql class. I would like to detect if it is new (meaning an insert will be done) or if there are any pending changes (update will be done). I realize that I can do this by using a partial class and hooking into the on change events for each property. However that is a lot of maintenance for a class that is constantly changing.
Is there a better way?
You can check through the
DataContextclass.First, check the
ObjectTrackingEnabledproperty on theDataContext. If it returns false, then the object is not being tracked by the context.Then, call the
GetChangeSetmethod on theDataContext. From there, compare the references exposed by theDeletes,Updates, andInsertsproperties against your object.If the reference is found in any of those lists, then your object is being tracked by that list and you can proceed from there.