I have some code that looks a little like this:
var query = from row in context.Orders.Include("Customer")
where row.Id = 5
select row;
var result = query.FirstOrDefault();
if(result == null)
{
result = new Order {CustomerId=5,...}
context.Orders.Add(result);
context.SaveChanges();
// at this point result.Customer is null
// and is not lazy-loaded by EF on access
}
return result;
As you can see on above code then if the insert happens then the Customer property of the result is not included, giving me an error later on in my code.
How can I ensure that result.Customer
is loaded from the database when result
is inserted? Can I do so without manually selecting it?