title: How to use joins with generic repository pattern - Entity Framework
body: <p>I have a <code>Generic Repository</code> like below which handles my <code>CRUD</code>, for a single entity its easy to use, problem starts when i try to join my <code>POCOs</code>.</p>
  
  <p>Lets say I have these POCO, they are mapped using fluent api (many to many and One to many relation) :</p>
  
  <pre><code>public class Student
  {
      public Student() 
      {
          this.Courses = new HashSet&lt;Course&gt;();
      }
  
      public int StudentId { get; set; }
      public string StudentName { get; set; }
  
      //FKs 
      public virtual Standard Standard { get; set; }
      public int StdandardRefId { get; set; }
  
      public virtual ICollection&lt;Course&gt; Courses { get; set; }
  }
  
  public class Course
  {
      public Course()
      {
          this.Students = new HashSet&lt;Student&gt;();
      }
  
      public int CourseId { get; set; }
      public string CourseName { get; set; }
  
      public virtual ICollection&lt;Student&gt; Students { get; set; }
  }
  public class Standard
  {
      public Standard()
      {
          Students = new List&lt;Student&gt;();
      }
      public int StandardId { get; set; }
      public string Description { get; set; }
  
      public virtual ICollection&lt;Student&gt; Students { get; set; }
  }
  </code></pre>
  
  <p>Mapping:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      //Many-To-Many
      modelBuilder.Entity&lt;Student&gt;()
                  .HasMany&lt;Course&gt;(s =&gt; s.Courses)
                  .WithMany(c =&gt; c.Students)
                  .Map(cs =&gt;
                          {
                              cs.MapLeftKey("StudentRefId");
                              cs.MapRightKey("CourseRefId");
                              cs.ToTable("StudentCourse");
                          });
          //One-To-Many
          modelBuilder.Entity&lt;Student&gt;()
                  .HasRequired&lt;Standard&gt;(s =&gt; s.Standard)
                  .WithMany(s =&gt; s.Students)
                  .HasForeignKey(s =&gt; s.StandardId);
  
  }
  </code></pre>
  
  <p>Generic Repository:</p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt;
      where T : class, IDisposable
  {
      internal MyDbContext context;
      internal DbSet&lt;T&gt; dbSet;
  
      public Repository()
      {
          context = new MyDbContext();
          this.dbSet = context.Set&lt;T&gt;();
      }
  
      public bool Add(T entity)
      {
          var query = dbSet.Add(entity);
  
          if (query != null)
              return true;
          return false;
      }
  
      public bool Update(T entity)
      {
          dbSet.Attach(entity);
          var query = context.Entry(entity).State = EntityState.Modified;
  
          if (query == EntityState.Modified)
              return true;
          return false;
      }
      public bool Delete(T entity)
      {
          var query = dbSet.Remove(entity);
  
          if (query != null)
              return true;
          return false;
      }
      public bool Delete(Guid id)
      {
          var query = dbSet.Remove(dbSet.Find(id));
  
          if (query != null)
              return true;
          return false;
      }
      public T GetById(Guid id)
      {
          var query = dbSet.Find(id);
  
          if (query != null)
              return query;
          else
              return null;
      }
      public ICollection&lt;T&gt; GetAll()
      {
          return dbSet.AsEnumerable&lt;T&gt;().ToList();
      }
  
      public void Save()
      {
          context.SaveChanges();
      }
      public void Dispose()
      {
          if (context != null)
          {
              context.Dispose();
              context = null;
          }
      }
  }
  </code></pre>
  
  <p>Now if i want to join Standard to Many-to-Many Table how may i be able to do this?</p>