0

i have a problem with some data i retrievied from db with linq. When I try to access data I obtain the following exception: System.ObjectDisposedException : The istance of ObjectContext was deleted and is not possible to use it again for action that need a connection. This is the code:

using (ProvaDbEntities DBEntities =     
    new ProvaDbEntities(Utilities.ToEntitiesConnectionString()))

            {
                ObjectQuery<site> sites = DBEntities.site;

                IEnumerable<site> q = from site in sites

                                      select site;


                {
                    ObjectQuery<auction> auctions = DBEntities.auction;

                    IEnumerable<auction> q1 = from auction in auctions

                                              where auction.site == this.Name

                                              select auction;

                    IEnumerable<IAuction> res = q1.Cast<IAuction>();

                    return res;

            }
        }
        catch(Exception e)
        {
            throw new UnavailableDbException("[GetAuctions]" + e.Message);
        }

Someone can help me??? Tanks Fabio

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
trifabbio
  • 1
  • 1

1 Answers1

3

Yes - you're returning a result which will be lazily evaluated - but you're disposing of the data context which would be used to fetch the results.

Options:

  • Load the results eagerly, e.g. by calling ToList on the result
  • Don't dispose of the context (I don't know what the situation is in the Entity Framework; you could get away with this in LINQ to SQL, but it may not be a good idea in EF)
  • Dispose of the context when you're finished with the data

In this case I'd suggest using the first option - it'll be safe and simple. As you're already filtering the results and you're casting to IEnumerable<IAuction> anyway, you're unlikely to get the normal downsides of materializing the query early. (If it were still IQueryable<T>, you'd be throwing away the ability to add extra bits to the query and them still be translated to SQL.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi Jon, I tried the firf option but I obteined an invalidCastException.. You mean somethink like this? //IEnumerable res = q1.Cast(); IEnumerable res2 = q1.ToList().Cast(); return res2; – trifabbio Jul 10 '10 at 18:02
  • @trifabbio: Not quite - you don't need to call `Cast` again. But if you're getting a cast exception, that suggests the objects being created just aren't implementations if `IAuction`. – Jon Skeet Jul 11 '10 at 06:40
  • Is the same problem that I (you :) ) solved here http://stackoverflow.com/questions/3075393/how-to-cast-list-to-enumerable .. I'm confused :( – trifabbio Jul 11 '10 at 16:34
  • Hi jon I'm still blocked here.. Now the situation is this: If I do IEnumerable res = q1.Cast(); I have an exception because ObjectContext was deleted. If I convert my result to a list I have an exception because NET 3.5 or lower - doesn't support generic covariance. Tank you for your answers Fabio – trifabbio Jul 15 '10 at 20:31
  • @trifabbio: Convert it to a list and *then* call `Cast`. You can do that later cast at any point, because the data's already been grabbed by then. – Jon Skeet Jul 15 '10 at 20:44