Suppose I have code like:
public void TestWhenSqlFires()
{
var db = new Db(); // EF DbContext
var items = db.SimpleObjects; // SimpleObject implements interface IId
var byId = WhereId(items, 1);
var array = byId.ToArray();
}
protected IEnumerable<IId> WhereId(IEnumerable<IId> items, int id)
{
return items.Where(i => i.Id == id);
}
At what line in TestWhenSqlFires() will SQL actually be run against the database?
(This is a question that spun off from comments on this answer)
One way to find out and test for yourself:
Open SQL Server Management Studio, open a new query, select the database EF will be running against and run this query:
This tells you the past 10 queries that have run against the database.
Set a breakpoint on the first line of TestWhenSqlFires(), run your code, then run the above query after stepping over each line. You'll find:
The final SQL query is EF actually fetching the data. The prior queries are just it warming up - verifying the database exists, that it's using EF5 Migration History, and that it matches the current Migration History hash.
So the answer is - the 4th line, after
.ToArray()
is called (or any call that enumerates the collection, like .ToList(), foreach, etc). Notably passing it to a method that accepts IEnumerable, even if there's a specific Generic involved, does not enumerate the collection, and so does not fire off SQL any earlier than necessary.