How to determine if a Linq object was bound to a data context?

Using Linq-to-Sql:

MyClass obj;
...
// need to delete this object
dataContext.GetTable(obj.GetType()).DeleteOnSubmit(obj);

BUT

I do not know if it was attached objto a data context. And if it is not, the last call to DeleteOnSubmit throws an exception.

There should be an easy way to tell if it is attached objto dataContext, but I don't see anything obvious.

How do you do this?

+3
source share
3 answers

While you can take a look at your design, as this should be something that you can determine deterministically, this can be done.

, , , (, L2S, "" ), , Context.Services.ChangeTracker.GetTrackedObject, internal. , , - Table.GetOriginalEntityState, . null, ( ). null, ().

, , , Reflector, , .

+4

Link2SQL, ? . ? obj , dataContext.

. , .

0

, , - PropertyChanging PropertyChanged.

, :


public virtual void Detach()
{
   PropertyChanging = null;
   PropertyChanged = null;
} 

, , .

0

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


All Articles