Eager-loading using LINQ to SQL with Include()
<p>I have spent 2 days bashing my head against this problem, and I can't seem to crack it (the problem that is).  The same code was working fine until I added database relationships, and I have since read a <em>lot</em> about lazy-loading.</p>

<p>I have two database tables with a 1:1 relationship between them.  <code>PromoCode</code> table tracks codes, and has a PK column named <code>id</code>.  <code>CustomerPromo</code> table has a column <code>PromoId</code> which is linked to the <code>PromoCode</code> table <code>id</code>.  These two tables have no other relationships.  I generated all this in SQL Server Management Studio, then generated the model from the database.</p>

<p>To make matters slightly more complicated, I'm doing this inside a WCF data service, but I don't believe that should make a difference (it worked before database relationships were added).  After enabling logging, I always get an Exception in the log file with text:</p>

<blockquote>
  <p>DataContext accessed after Dispose.</p>
</blockquote>

<p>My function currently returns all entries from the table:</p>

<pre><code>using (MsSqlDataContext db = new MsSqlDataContext())
{
    // This causes issues with lazy-loading
    return db.PromoCodes.ToArray();
}
</code></pre>

<p>I have read numerous articles/pages/answers and they all say to use the <code>.Include()</code> method.  But this doesn't work for me:</p>

<pre><code>return db.PromoCodes.Include(x =&gt; x.CustomerPromos).ToArray();
</code></pre>

<p>I've tried the "magic string" version as well:</p>

<pre><code>return db.PromoCodes.Include("CustomerPromos").ToArray();
</code></pre>

<p>The only code I've managed to get to work is this:</p>

<pre><code>PromoCode[] toReturn = db.PromoCodes.ToArray();

foreach (var p in toReturn)
    p.CustomerPromos.Load();

return toReturn;
</code></pre>

<p>I've tried added a <code>.Where()</code> criteria to the query, I've tried <code>.Select()</code>, I've tried moving the <code>.Include()</code> after the <code>.Where()</code> (<a href="https://social.msdn.microsoft.com/Forums/en-US/76bf1f22-7674-4e1e-85d3-68d29404e8db/include-is-ignored-in-a-subquery?forum=adodotnetentityframework" rel="noreferrer">this answer</a> says to do it last, but I think that's only due to nested queries).  I've read about scenarios where <code>.Include()</code> <a href="http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework" rel="noreferrer">will silently fail</a>, and after all this I'm no closer.</p>

<p>What am I missing?  Syntax problem?  Logic problem?  Once I get this "simple" case working, I also need to have nested <code>Include</code>s (i.e. if <code>CustomerPromo</code> table had a relationship to <code>Customer</code>).</p>

<p><strong><em>Edit</em></strong><br>
Including <em>all</em> relevant code.  The rest is either LINQ to SQL, or WCF Data Services configuration.  This is all there is:</p>

<pre><code>[WebGet]
[OperationContract]
public PromoCode[] Test()
{
    using (MsSqlDataContext db = new MsSqlDataContext())
    {
        return db.PromoCodes.Include(x =&gt; x.CustomerPromos).ToArray();
    }
}
</code></pre>

<p>If I call that through a browser directly (e.g. <code>http://&lt;address&gt;:&lt;port&gt;/DataService.svc/Test</code>) I get a reset connection message and have to look up the WCF logs to find out "<code>DataContext accessed after Dispose.</code>".  If I make the same query through an AJAX call in a webpage I get an AJAX error with status <code>error</code> (that's all!).</p>