title: Lazy Loading - (Entity Framework) The Underlying Connection was Closed Unexpectedly
body: <p>I'm trying to implement Lazy loading using Entity framework. I'll admit right upfront I've never done this and am struggling. So everything I'm trying is based on articles I've found here. Basically I'm now getting The underlying connection was closed unexpectedly error message.</p>
  
  <p>When I do a WCF trace, it indicates that there was an error while trying to serialize parameter <a href="http://tempuri.org...Consider" rel="nofollow">http://tempuri.org...Consider</a> using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types.</p>
  
  <p>So I've researched the DataContractResolver and am unclear as to how to implement this and further am unclear as to how I know if I'm using a DataContractSerializer. Like I said I'm new to this.</p>
  
  <p>Here is my current code:</p>
  
  <pre><code> public OMBCase GetOMBCaseByCaseId(long caseId)
      {
          //using (AWCTSDBEntities context = new AWCTSDBEntities())
          AWCTSDBEntities context = new AWCTSDBEntities();
          //{
          try
          {
              context.Configuration.ProxyCreationEnabled = true;
              context.Configuration.LazyLoadingEnabled = true;
              context.Configuration.AutoDetectChangesEnabled = false;
  
              ((IObjectContextAdapter)context).ObjectContext.CommandTimeout = 120;
              context.Database.Log = s =&gt; System.Diagnostics.Debug.WriteLine(s);
  
              var caseRecQuery = (from c in context.Cases
                                  where (c.CaseId == caseId &amp;&amp; c.SystemTypeCode == SystemCodes.OMB)
                                  select c).OfType&lt;OMBCase&gt;();
  
              var caseRecord = caseRecQuery.Single();
              var soldierDetails = caseRecord.Soldier;
              var soldierAddress = soldierDetails.Addresses.ToList();
              foreach (var p in soldierDetails.Addresses)
              {
                  var soldierAddressPurpose = p.AddressPurposes;
                  foreach (var ap in soldierAddressPurpose)
                  {
                      var purpose = ap;
                  }
              }
              var soldierTelehone = soldierDetails.Telephones.ToList();
              var soldierEmail = soldierDetails.Emails.ToList();
              var soldierContacts = soldierDetails.Contacts.ToList();
              foreach (var ca in soldierDetails.Contacts)
              {
                  var contactAddress = ca.Addresses;
                  foreach (var cp in contactAddress)
                  {
                      var contactAddressPurposes = cp.AddressPurposes;
                  }
              }
              foreach (var ce in soldierDetails.Contacts)
              {
                  var contactEmails = ce.Emails;
              }
              foreach (var ct in soldierDetails.Contacts)
              {
                  var contactTelephone = ct.Telephones;
              }
              var personInterviewed = caseRecord.PersonInterviewed;
              var createdByUser = caseRecord.CreatedByUser;
              context.Dispose();
              return (caseRecord);
              //
          }
          catch (NullReferenceException)
          {
              context.Dispose();
              return (null);
          }
          catch (Exception e)
          {
              context.Dispose();
              WriteToLogFile(e);
              return (null);
              //    }
          }
  
  
      }
  </code></pre>
  
  <p>Any guidance that you can provide as to where to go from here would be greatly appreciated. </p>
  
  <p>Thanks in advance for any help you can provide.</p>
  
  <p>Rodney</p>