How to determine if a linq to sql object is new, changed, or immutable?

I have one linq to sql class. I would like to determine if this is new (which means the insert will be completed) or if there are any pending changes (the update will complete). I understand that I can do this using the partial class and connecting to the on change events for each property. However, this is a lot of service for an ever-changing class.

Is there a better way?

+4
source share
2 answers

You can check the DataContext class.

First check the ObjectTrackingEnabled property on the DataContext . If it returns false, then the object is not tracked by the context.

Then call the GetChangeSet method on the DataContext . From there, compare the links provided by the Deletes , Updates and Inserts properties against your object.

If the link is found in any of these lists, then your object is tracked by this list, and you can go from there.

+7
source

Check if object id is 0.

 if someId = 0 Insert(); else Update(); 

Simples

-1
source

Source: https://habr.com/ru/post/1311655/


All Articles