I use Entity Framework. Let's say I have a method:
public IEnumerable<User> GetUsers() // return IEnumerable<User>
{
using (var context = new AppDbContext())
{
return context.Users;
}
}
Now when I call:
var users = GetUsers();
Will this operation perform a database query or not?
Not if you don't do anything with it, no.
However, if you try to iterate over the results (or call Count()
etc) then it will try to make a database call... and I'd expect it to then fail, because you've disposed of the context at that point.
See more on this question at Stackoverflow