ASP.Net Entity Framework, objectcontext error

Posted by Chris Klepeis on Stack Overflow See other posts from Stack Overflow or by Chris Klepeis
Published on 2010-05-21T17:49:46Z Indexed on 2010/05/21 17:50 UTC
Read the original article Hit count: 702

I'm building a 4 layered ASP.Net web application. The layers are:

  1. Data Layer
  2. Entity Layer
  3. Business Layer
  4. UI Layer

The entity layer has my data model classes and is built from my entity data model (edmx file) in the datalayer using T4 templates (POCO). The entity layer is referenced in all other layers.

My data layer has a class called SourceKeyRepository which has a function like so:

public IEnumerable<SourceKey> Get(SourceKey sk)
{
    using (dmc = new DataModelContainer())
    {
        var query = from SourceKey in dmc.SourceKeys
                    select SourceKey;

        if (sk.sourceKey1 != null)
        {
            query = from SourceKey in query
                    where SourceKey.sourceKey1 == sk.sourceKey1
                    select SourceKey;
        }

        return query;
    }
}

Lazy loading is disabled since I do not want my queries to run in other layers of this application. I'm receiving the following error when attempting to access the information in the UI layer:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

I'm sure this is because my DataModelContainer "dmc" was disposed. How can I return this IEnumerable object from my data layer so that it does not rely on the ObjectContext, but solely on the DataModel?

Is there a way to limit lazy loading to only occur in the data layer?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about objectdisposedexception