0

I have the following method:

    public void InsertOrUpdate(AmazonProduct amazonProduct)
    {
        var product = context.AmazonProducts
                        .Include(x => x.AmazonLowestOfferListings)
                        .Include(x => x.AmazonMyPrices)
                        .Include(x => x.AmazonProductCompetitivePrices)
                        .FirstOrDefault(p => p.ASIN == amazonProduct.ASIN);

        if (product == null)
        {
            // New entity
            context.AmazonProducts.Add(amazonProduct);
        }
        else
        {
            foreach (var lineItem in amazonProduct.AmazonLowestOfferListings)
            {
                context.AmazonLowestOfferListings.Add(lineItem);
            }

            context.Entry(product).CurrentValues.SetValues(amazonProduct);
        }
    }

But on the line,

foreach (var lineItem in amazonProduct.AmazonLowestOfferListings)

it gives me the error:

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

I dont get it.. I havent even added it to the context yet, and it still gives me this error? Any one any idea?

thanks Neil

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Neil Hosey
  • 465
  • 6
  • 23
  • Did you include `AmazonLowestOfferListings` in the `amazonProduct` passed to that method ? EF may be trying to lazy-load that collection, which needs a connection that has been disposed in this context. – Drewman Apr 26 '13 at 10:46
  • I cant, as AmazonProduct is just an object at that point. I havent added it to the context yet, if you know what i mean? – Neil Hosey Apr 26 '13 at 10:49
  • Where does the value for the amazonProduct parameter come from? Its context may have been disposed of, therefore you can't enumerate its AmazonLowestOfferListings – Netricity Apr 26 '13 at 10:49
  • its just a POCO i created and passed to the InsertOrUpdate() method of my repository – Neil Hosey Apr 26 '13 at 10:50
  • Sorry!! you are correct.. i forgot i Find() this product previously... thank you so much! – Neil Hosey Apr 26 '13 at 10:52
  • If I had a dollar each time that error poped up... I've posted my original comment as an answer. – Drewman Apr 26 '13 at 10:56

1 Answers1

1

Did you include AmazonLowestOfferListings in the amazonProduct passed to that method ?

EF may be trying to lazy-load that collection, which needs a connection that has been disposed in this context.

Drewman
  • 947
  • 11
  • 23