title: entity framework Code First One-to-Many relationships in Web.API
body: <p>I'm trying to create a one to many relationship using entity framework to convert the json object using Web.API return.</p>
  
  <p>I am creating a scenario that a User has many features emails.</p>
  
  <p>However json file in the User entity that brings contains a list of Emails that appear always empty.</p>
  
  <p><strong>User Entity</strong></p>
  
  <pre><code>[DataContract]
  public class UsuarioEntity : BaseEntity
  {
      [DataMember]
      public string UserName { get; set; }
  
      [DataMember]
      public string Password { get; set; }
  
      [DataMember]
      public bool Ativado { get; set; } 
  
      [DataMember] 
      public ICollection&lt;EmailEntity&gt; Emails { get; private set; }
  }
  </code></pre>
  
  <p><strong>Email Entity</strong></p>
  
  <pre><code> [DataContract]
  public class EmailEntity : BaseEntity
  {
  
      [DataMember]
      public string Email { get; set; }
  
      [DataMember]
      public bool Ativo { get; set; }
  
      [DataMember]
      public bool Principal { get; set; }
  
      [DataMember]
      public DateTime DataCadastro { get; set; }
  
      [DataMember]
      public UsuarioEntity Usuario { get; set; }
  
  } 
  </code></pre>
  
  <p><strong>Fluent API</strong> </p>
  
  <p><strong>UsuarioConfiguration</strong></p>
  
  <pre><code>            ToTable("tbl_usuarios");
  
          HasKey(a =&gt; new {a.Id, a.UserName});
  
          Property(a =&gt; a.Id)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
              .HasColumnName("cd_id_usuario");
  
          Property(a =&gt; a.UserName)
              .HasMaxLength(32)
              .HasColumnName("nm_usuario");
  
          Property(a =&gt; a.Password)
              .IsRequired()
              .HasMaxLength(32)
              .HasColumnName("tx_senha");
  
          Property(a =&gt; a.DataCadastro) 
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)
              .HasColumnType("datetime")
              .IsRequired()
              .HasColumnName("dt_cadastro");
  
          HasMany(o =&gt; o.Emails)
              .WithRequired();
  </code></pre>
  
  <p><strong>EmailConfiguration</strong></p>
  
  <pre><code>            ToTable("tbl_email_usuario");
  
          HasKey(a =&gt; new { a.Id, a.Email });
  
          Property(a =&gt; a.Id)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
              .HasColumnName("cd_id_email");
  
          Property(a =&gt; a.Email)
              .HasMaxLength(150)
              .HasColumnName("nm_email");
  
          Property(a =&gt; a.DataCadastro)
              .HasColumnType("datetime")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)
              .IsRequired()
              .HasColumnName("dt_cadastro")
  </code></pre>
  
  <p>I tried to use EF with Web api this way:</p>
  
  <pre><code>public IQueryable&lt;UsuarioEntity&gt; Get()
  {
      PlainContext context = new PlainContext();
      return context.Usuarios;
  }
  </code></pre>
  
  <p>Except that the return was not expected</p>
  
  <pre><code>[{"Id":1,"Ativado":true,"DataCadastro":"\/Date(1357307720730-0200)\/","Emails":[],"Password":"senha456","UserName":"recapix"}]
  </code></pre>
  
  <p>I own and Registered Emails only they do not appear on Json.</p>