views:

38

answers:

1

Hi, 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

A: 

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
Hi Jon, I tried the firf option but I obteined an invalidCastException..You mean somethink like this?//IEnumerable<IAuction> res = q1.Cast<IAuction>();IEnumerable<IAuction> res2 = q1.ToList().Cast<IAuction>();return res2;
trifabbio
@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
Is the same problem that I (you :) ) solved herehttp://stackoverflow.com/questions/3075393/how-to-cast-list-to-enumerable ..I'm confused :(
trifabbio
Hi jon I'm still blocked here.. Now the situation is this:If I do IEnumerable<IAuction> res = q1.Cast<IAuction>();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 answersFabio
trifabbio
@trifabbio: Convert it to a list and *then* call `Cast<IAuction>`. You can do that later cast at any point, because the data's already been grabbed by then.
Jon Skeet