1

I have a db as below:

enter image description here

I am using EF and trying to get the object. Under normal circumstances after selecting PRODUCT Entity from database I can reach ACCESSLEVEL Entity of PRODUCT Entity like below:

In selecting, I use INCLUDE (NOTE: the code below works fine!)

//In a method located in some BL layer
public ProductCollection Load()
{
  ProductCollection  Result = new ProductCollection(); //Derived from List
using (var Context = base.Entities)
            {
                    collection = from ItemProduct in Context.Product
                                     .Include("AccessLevel");

                    return Result.AddRange(collection);
            }
}
//In page load in aspx.cs file
foreach(var product in BL.Load())
{
    Response.Write(product.AccessLevel.Name);
}

However, in here when I do the thing below it does NOT work!

//In a method located in some BL layer
    public ProductCollection Load()
    {
     ProductCollection  Result = new ProductCollection(); //Derived from List
    using (var Context = base.Entities)
                {

                        collection = from ItemProduct in Context.Product
                                         .Include("AccessLevel")
                                         .Join(Context.Product_Category_Map.Where(c => c.ProductCategoryId == 3),
                                    product => product,
                                    map => map.Product,
                                    (product, map) => product
                               ));
;

                        return Result.AddRange(collection);
                }
    }
    //In page load in aspx.cs file
    foreach(var product in BL.Load())
    {
//I Get Exception here and cannot react the included object
        Response.Write(product.AccessLevel.Name);
    }

The Exception is:

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

What I finally want is to get products by the given ProductCategory Id.

How can I do that?

Thanks in advance.

E-A
  • 1,995
  • 6
  • 33
  • 47

2 Answers2

1

I think you could add a .ToList() to the end of the collection, i.e.

return collection.ToList();

This will make the results available even after the using statement closes the context, which I believe is what is causing your problem.

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
  • I updated the simpllifed code. I already was doing that in fact. – E-A Oct 04 '12 at 14:53
  • Interestingly this worked. "ToList()" .. Thank you E.J. I said interestingly because I was using it as a collection which was derived from List ... Anyways, thanks. – E-A Oct 05 '12 at 13:35
0

I think you should replace this:

using (var Context = base.Entities)

with this:

using (var Context = new base.Entities)

If you want to know more, take a look here: Best way to initialize an entity framework context?

Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • Thank you Laniel. I in fact in the base I get new instance always. The reason why I did that I may have other operations during instantiating thats why I used base.Entities. But its always a new instance. – E-A Oct 05 '12 at 06:06