开发者

How do I tell if a linq to sql object is new, modified or unchanged?

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 DataContext class.

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

Then, call the GetChangeSet method on the DataContext. From there, compare the references exposed by the Deletes, Updates, and Inserts properties 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.


Check if the id of the object is 0.

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

Simples

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜