I have a simple question but didn't find an answers.
If I do so
var result = _db.Table.Include(t => t.Child).Where(t => t.Id == id).Single();
when join is calling?
After it found my entity or it includes every child during SQL looking for the row?
Lets see at the example based on simple db model:
public class Head
{
//... columns
public virtual Child {get; set;}
public Guid? ChildId {get; set;}
}
void main()
{
//The first version of code
var child = _db.Head.Include(h => h.Child)
.FirstOrDefault(//boring staff but we don't need child here)
?.Child;
if (child != null)
foo(child);
//The second one
var head = _db.Head.FirstOrDefault(//boring staff);
if (head != null && head.ChildId.HasValue)
foo(head.Child); // I know here we make a new request to our db
}
Which of two options are more productive?
I'm worry about "extra childs loading by SQL" when I need only one object based on filters query of parent table.
Thanks in advance!
.Single(). You could easily have figured this out for yourself by profiling the database and watching the queries come in. You should learn to do that anyway.