title: entity framework serialize
body: <p>I'm having trouble making an client-server app.
  I'm working with <strong>Entity Framework(EF)</strong> and I need to serialize an object to send it via sockets that contains List attributes. 
  I'm using <strong>XMLSerialization</strong> for the Serialization part.</p>
  
  <p><strong>My problem is:</strong> When I try to Serialize a new Survey object and the OPTIONs List is Empty I can Serialize the object SURVEY. But, when I add an OPTION object to the SURVEY.OPTIONS list I cannot Serialize the object.</p>
  
  <p><strong>One of the classes that EF auto-generates from the Entity-Relationship Diagram is:</strong></p>
  
  <pre><code>public partial class SURVEY
  {
      public SURVEY()
      {
          this.OPCIONs = new List&lt;OPTION&gt;();
      }
  
      public int id_survey { get; set; }
      public System.DateTime initial_date { get; set; }
      public System.DateTime end_date { get; set; }
      public virtual List&lt;OPTION&gt; OPTIONs { get; set; }
  }
  </code></pre>
  
  <p><strong>I'm using this Code for get a Survey from the Database:</strong></p>
  
  <pre><code>DateTime actualDate = new DateTime().Today;
  private static ComedorCaniaDBContext context = new ComedorCaniaDBContext();
  Survey survey = context.SURVEYs.Create()
  survey = (SURVEY)context.SURVEYs
                      .Include("Options")
                      .Where(e =&gt; e.initial_date &lt; actualDate &amp;&amp; e.end_date &gt; actualDate)
                      .FirstOrDefault();
  </code></pre>
  
  <p><strong>I'm using this Code for Serialization:</strong></p>
  
  <pre><code>public static Byte[] ObjectToByteArray&lt;T&gt;(T obj)
      {
          try
          {
              using (MemoryStream ms = new MemoryStream())
              {
                  XmlSerializer xmlS = new XmlSerializer(typeof(T));
                  xmlS.Serialize(ms, obj);
  
                  return ms.ToArray();
              }
          }
          catch
          {
              return null;
          }
      }
  </code></pre>
  
  <p>I'll appreciate your help.
  Thanks.</p>