Why do I need ToList () to avoid local context errors?

I am writing code to access a database using EntityFrameWork. The code:

public IEnumerable<Rows> GetRows(int id) { using (var context = new ApplicationDbContext()) { var repository = new EntityFrameWorkRepository<int, RowEntity>(context); //need a ToList() here to prevent disposed dbcontext errors return repository.GetRowsFromDb(id).ToList(); } } 

GetRowsFromDb () uses LINQ to query the database and filter the results using an identifier.

I originally wrote this method without calling ToList (), but when I tried to access the objects in the returned IEnumerable, I would get an exception from an existing dbcontext. I do not understand how this code fixes everything, although it works. I believe that ToList () is a deep copy of the object and that it possibly provides the necessary separation from context / database, but, of course, should the original object be useful?

+8
c # linq entity-framework asp.net-mvc-5
source share
4 answers

The reason you need to call ToList , ToArray or some other method that lists the data returned by EF is because the query execution in LINQ is delayed: the data is not processed until you take it explicitly. After your method returns the context through which the request data was received, it will be closed (your using block will take care of this quickly), throwing an exception that you see.

This is done so that the code does not waste time processing data that you do not need. For example, you can write code that starts reading data on the client side and stops in the middle. If the execution of the request was not delayed, you would spend time and memory on receiving the tail of the request only to throw it out. Delayed execution puts you in control: you decide what data you want to keep when you go, or transfer the entire collection to memory based on what you plan to do with the data.

+8
source share

If you do not call .ToList() , the enumerated will be evaluated after the completion of your using clause, and thus the data context will be deleted before the request is evaluated.

IMO, you should consider making your repository processed (by calling .ToList() ), because otherwise this problem is representative of leakage of implementation information.

+7
source share

Without ToList() you return the counter of a non-actual collection of objects. Actual objects are retrieved when trying to access the collection. But in this case, you need a context and a repository, because you are accessing them from the database. But since it has already gone beyond the scope of the using clause, both of them throw an exception.

+1
source share

I assume ToList () is doing a deep copy of an object

Not really - before you call ToList all you have is a request. you won’t get the results until you list them or turn them into a specific collection using ToList , ToArray , etc

Surely the source object should be used?

No - it was deleted, this is your way to tell the system that the object has completed its work and is no longer needed. The fact that you still have an outstanding request does not keep the context operational.

+1
source share

All Articles