0

I've got a problem with returning the result of DbContext query in a method of data access layer. I'm doing this:

public IEnumerable<Person> GetAllPeople()
{
    IEnumerable<Person> people;

    using (var context = new AdressingContext())
    {
        people = context.People.ToList();
    }

    return people;
} 

And when I want to print the output in Main:

class Program
{
    static void Main(string[] args)
    {
        ContactsRepository repository = new ContactsRepository();
        var people = repository.GetAllPeople();

        foreach (var item in people)
        {
            Console.WriteLine(item);
        }

        Console.ReadKey();
    }
}

The exception is thrown

The operation cannot be completed because the DbContext has been disposed.

I know that in the end of using block the objects created inside are disposed, but shouldn't

people = context.People;

copy those objects? Could you propose an elegant way to solve this?

Edit: exception is thrown in Person.ToString() in if() statement.

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

public override string ToString()
{
    if (ContactInformation == null)
    {
        return $"PersonId={PersonId}, FirstName={FirstName}, LastName={LastName}, Sex={Sex}, Birthday={Birthday}, Description: {Description}";
    }

    return $"PersonId={PersonId}, FirstName={FirstName}, LastName={LastName}, Sex={Sex}, Birthday={Birthday}, Contact: {ContactInformation.ToString()}, Description: {Description}";
} 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DominikS
  • 361
  • 3
  • 13
  • 2
    Are you really using `people = context.People.ToList()` there? Or just `people = context.People` as in your very last code excerpt? – poke Jan 07 '18 at 13:44
  • When calling `ToList()` that EF loads value to in-memory collection. – Eldho Jan 07 '18 at 13:46
  • What is `ContactInformation`? Is this maybe another entity that is linked to your `Person` type? In that case, you will have to do `context.People.Include(p => p.ContactInformation).ToList()` – poke Jan 07 '18 at 13:56
  • Yes, I was using people = context.People.ToList(), but there is still an exception. – DominikS Jan 07 '18 at 13:57
  • Correct, ContactInformation is another entity linked to Person type, but your code is not compiling because it says that p => p.ContactInformation cannot be converted from lambda to string – DominikS Jan 07 '18 at 14:00
  • Thank you, I've read about Include() method and this really helped me, the solution was people = context.People.Include("ContactInformation").ToList(); – DominikS Jan 07 '18 at 14:07
  • See this question or many like it: https://stackoverflow.com/questions/18398356/solving-the-objectcontext-instance-has-been-disposed-and-can-no-longer-be-used/18398729#18398729 – Jamie Twells Jan 07 '18 at 17:37

1 Answers1

1

you have lazy loading turned on and you are getting information from a navigation property (another table). thus it will try to load that data when you access it but you already disposed of context. either use .include or turn off lazy loading.

Daniel Lorenz
  • 4,178
  • 1
  • 32
  • 39