.Net Entity objectcontext thread error

Posted by Chris Klepeis on Stack Overflow See other posts from Stack Overflow or by Chris Klepeis
Published on 2010-05-24T13:33:16Z Indexed on 2010/05/24 13:51 UTC
Read the original article Hit count: 299

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?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about entity-framework