title: Entity Framework, LINQ, and populating parent models and their children
body: <p>I'm new to EF/LINQ, and despite searching I can't seem to figure this out. I'm working with an existing database. I have Parents, and they have a one-to-many relationship with Children. I want to use the database to populate a list of the Parents <em>AND</em> populate their children. So in the end I'll have a collection of all the data for the parents, each of which has a list of children also populated with data. But I can't seem to get the parent-child relationship working. I know how to do this:</p>
  
  <pre><code>var query = (from p in myDbContext.Parents
                select p);
  </code></pre>
  
  <p>but that doesn't give me the Child data. I figured out how to do this:</p>
  
  <pre><code>var query = (from p in myDbContext.Parents join c in myDbContext.Children
              on p.Id equals c.ParentId into gj
              from sub in gj.DefaultIfEmpty()
              select p);
  </code></pre>
  
  <p>but just like a left outer join, I get repeats of parent records when they have multiple children. I could loop through them to build the data I want piece by piece, but I'm guessing there's a way that's actually based on the relationship? I eventually will need to handle many-to-many's as well. My models are:</p>
  
  <pre><code>public class Parent
  {
      public int Id { get; set; } 
      public string Name { get; set; }
      public virtual ICollection&lt;Child&gt; Children { get; set; }
  
      /* and a bunch of other fields */
  
      public Parent()
      {
          this.Children = new List&lt;Child&gt;();
      }
  }
  
  public class Child
  {
      public int Id { get; set; }
      public int ParentId { get; set; }        
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>My mapping:</p>
  
  <pre><code>public class ParentMap : EntityTypeConfiguration&lt;Parent&gt;
  {
      public ParentMap()
      {
              this.HasKey(t =&gt; t.Id);
  
              HasMany(t =&gt; t.Children)
                  .WithRequired()
                  .HasForeignKey(t =&gt; t.ParentId);
  
              this.Property(t =&gt; t.Name)
                  .HasMaxLength(50);
  
              this.ToTable("Parents");
              this.Property(t =&gt; t.Id).HasColumnName("ParentId");
              this.Property(t =&gt; t.Name).HasColumnName("Name");
  
              /* and a bunch of other properties */
          }
  
  
  }
  
  public class ChildMap : EntityTypeConfiguration&lt;Child&gt;
  {
      public ChildMap()
      {
          this.HasKey(t =&gt; t.Id);
          this.Property(t =&gt; t.Name)
              .HasMaxLength(50);
  
          this.ToTable("Children");
          this.Property(t =&gt; t.Id).HasColumnName("ChildId");
          this.Property(t =&gt; t.ParentId).HasColumnName("ParentId");
          this.Property(t =&gt; t.Name).HasColumnName("Name");
      }
  
  }
  </code></pre>
  
  <p>My data context:</p>
  
  <pre><code>public class myContext : DbContext
  {
      public DbSet&lt;Parent&gt; Parents { get; set; }
      public DbSet&lt;Child&gt; Children { get; set; }
  
      public myContext() :
          base("Name=myContext")
      {
          Configuration.ProxyCreationEnabled = false;
      }
  
      static myContext()
      {
          Database.SetInitializer&lt;myContext&gt;(null);
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Configurations.Add(new ParentMap());
          modelBuilder.Configurations.Add(new ChildMap());
      }        
  }
  </code></pre>