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

后端 未结 4 839
面向向阳花
面向向阳花 2021-01-11 18:30

I\'m writing some code to access a database using EntityFrameWork. The code is:

public IEnumerable GetRows(int id)
{
    using (var context = new         


        
4条回答
  •  旧巷少年郎
    2021-01-11 19:19

    Without ToList() you only return enumerator not actual collection of objects. The actual objects are fetched when you try to access the collection. But in this case what you need is a context and repository, because you accessing them from the database. But since it's already out of scope of the using clause both are disposed hence the exception.

提交回复
热议问题