.Net Entity objectcontext thread error
- by Chris Klepeis
I have an n-layered asp.net application which returns an object from my DAL to the BAL like so:
public IEnumerable<SourceKey> Get(SourceKey sk)
{
var query = from SourceKey in _dataContext.SourceKeys
select SourceKey;
if (sk.sourceKey1 != null)
{
query = from SourceKey in query
where SourceKey.sourceKey1 == sk.sourceKey1
select SourceKey;
}
return query.AsEnumerable();
}
This result passes through my business layer and hits the UI layer to display to the end users. I do not lazy load to prevent query execution in other layers of my application.
I created another function in my DAL to delete objects:
public void Delete(SourceKey sk)
{
try
{
_dataContext.DeleteObject(sk);
_dataContext.SaveChanges();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " " + ex.StackTrace + " " + ex.InnerException);
}
}
When I try to call "Delete" after calling the "Get" function, I receive this error:
New transaction is not allowed because
there are other threads running in the
session
This is an ASP.Net app. My DAL contains an entity data model. The class in which I have the above functions share the same _dataContext, which is instantiated in my constructor. My guess is that the reader is still open from the "Get" function and was not closed. How can I close it?