Can I access the Object State of a data entity in LINQ to SQL?
Hi
I have a method to reset data objects by calling DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, Entity);
.
Catch is that some of the objects passed to the method are objects that came from the datacontext and have been changed, but some others are new objects. The new objects have not been attached to the datacontext and are being discarded.
When DataContext.Refresh
is called on the unattached objects, I get a System.ArgumentException
since the DataContext is not familiar with it.
Is there a way to get the tracking state (as defined here: http://msdn.microsoft.com/en-us/library/bb386982.aspx) of an individual object, so I can figure out if it is开发者_运维技巧 a modified object or a new object?
Regards
You could do this:
var changes = DbContext.GetChangeSet();
if(changes.Updates.Contains(EntityToCheck))
//Changed state
else if(changes.Inserts.Contains(EntityToCheck))
//New state
else if(changes.Deletes.Contains(EntityToCheck))
//Delete state
精彩评论