title: Entity Framework dependencies loading
body: <p>I have a long-time burning question about how to avoid null errors with data queried via Entity Framework (version 6 - not Core yet, sadly).</p>
  <p>Let's say you have a table <code>Employees</code>, and it has a relationship with another table, <code>EmployeePayments</code> (one employee has many employee payments).</p>
  <p>On your <code>Employee</code> domain object you create a property <code>TotalPayments</code> which relies on you having loaded the <code>EmployeePayments</code> for that object.</p>
  <p>I try to ensure that any time I do a query, I &quot;include&quot; the dependency, for example:</p>
  <pre><code>var employees = context.Employees.Include(e =&gt; e.EmployeePayments);
  </code></pre>
  <p>The problem is, I have a lot of queries around the place (I use the generic repository pattern, so I call repository functions like <code>GetAll</code> or <code>GetSingle</code> from my service library), and so that's a lot of places to remember to add the includes. If I don't include them, I run the risk of having a null exception if the <code>TotalPayments</code> property is used.</p>
  <p>What's the best way to handle this?</p>
  <p>Note 1: we have a lot of tables and I don't really want to have to revert to using specific repositories for each one, we take advantage of the generic repository in a lot of ways.... but I will be happy to hear strong arguments for the alternative :)</p>
  <p>Note 2: I do not have lazy loading turned on, and don't plan on turning it on, for performance reasons.</p>