Access DataContext data from linq to SQL

I have a linq2sql datacontext that is being deleted. But when I check for null, I always have a false condition.

DataClasses1DataContext dc = new DataClasses1DataContext();
dc.Dispose();

another code further

if (dc == null) {
    // ALWAYS FALSE
}

How can I find out if the datacontext has been deleted?

UPDATE: Let me clarify. I get a datacontext, but sometimes the external code passes an object (which is not null but already located). I need to check if an object exists. I was thinking of something else than trying.

+3
source share
2 answers

The class DataContextdoes not provide any properties that can tell you whether it has been deleted.
However, you can do it yourself by overriding the method Dispose(bool):

public bool IsDisposed { get; private set; }
protected override void Dispose(bool disposing) {
    IsDisposed = true;
}
+5
source

Dispose() . dc - , , , - dc = null; Dispose(). , , .

dc - , :

using(var dc = new YourDataContext()) {
    ...
}
+1

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


All Articles