title: How to implement many to many relationship in EF with ASP.NET MVC 5
body: <p>I have just started working with EF and I faced this problem. I have a TeamSeason table with many to many relationship between teams and seasons.  I have also implemented GenericRepository to communicate with database. My models look like this:</p>
  
  <pre><code> public class Team : ITeam
   {
      public int Id { get; set; }
      [Required]
      public string Name { get; set; }
  
      public virtual ICollection&lt;Season&gt; SeasonsPlayed { get; set; }
   }
  
  public class Season : ISeason
  {
      public int Id { get; set; }
  
      [Required]
      public string LeagueName { get; set; }
  
      public ICollection&lt;Team&gt; Teams { get; set; }
  
      public void AddTeam(Team team)
      {
         Teams.Add(team);
      }
  }
  
  
   public class GenericRepository&lt;T&gt; : IGenericRepository&lt;T&gt; where T : class
  {
      protected ApplicationDbContext _context;
  
      public GenericRepository(ApplicationDbContext context)
      {
          _context = context;
      }
  
      public virtual void Add(T t)
      {
          _context.Set&lt;T&gt;().Add(t);
          _context.SaveChanges();
      }
  
      public virtual async Task AddAsync(T t)
      {
          _context.Set&lt;T&gt;().Add(t);
          await _context.SaveChangesAsync();
      }
  
      public virtual int Count()
      {
          return _context.Set&lt;T&gt;().Count();
      }
  
      public virtual async Task&lt;int&gt; CountAsync()
      {
          return await _context.Set&lt;T&gt;().CountAsync();
      }
  
      public virtual void Delete(T entity)
      {
          _context.Set&lt;T&gt;().Remove(entity);
          _context.SaveChanges();
      }
  
      public virtual async Task&lt;int&gt; DeleteAsync(T entity)
      {
          _context.Set&lt;T&gt;().Remove(entity);
          return await _context.SaveChangesAsync();
      }
  
      public T Get(int id)
      {
          return _context.Set&lt;T&gt;().Find(id);
      }
  
      public IQueryable&lt;T&gt; GetAll()
      {
          return _context.Set&lt;T&gt;();
      }
  
      public virtual async Task&lt;ICollection&lt;T&gt;&gt; GetAllAsync()
      {
          return await _context.Set&lt;T&gt;().ToListAsync();
      }
  
      public virtual async Task&lt;T&gt; GetAsync(int id)
      {
          return await _context.Set&lt;T&gt;().FindAsync(id);
      }
  
      public void Save()
      {
          _context.SaveChanges();
      }
  
      public async virtual Task&lt;int&gt; SaveAsync()
      {
          return await _context.SaveChangesAsync();
      }
  
      public void Update(T t, object key)
      {
          if (t == null)
          {
              return;
          }
  
          T exist = _context.Set&lt;T&gt;().Find(key);
          if (exist != null)
          {
              _context.Entry(exist).CurrentValues.SetValues(t);
              _context.SaveChanges();
          }
      }
  
      public virtual async Task UpdateAsync(T t, object key)
      {
          if (t == null)
          {
              return;
          }
  
          T exist = await _context.Set&lt;T&gt;().FindAsync(key);
          if (exist != null)
          {
              _context.Entry(exist).CurrentValues.SetValues(t);
              await _context.SaveChangesAsync();
          }
      }
  
      private bool disposed = false;
      protected virtual void Dispose(bool disposing)
      {
          if (!disposed)
          {
              if (disposing)
              {
                  _context.Dispose();
              }
              disposed = true;
          }
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  }
  </code></pre>
  
  <p>EF generated TeamSeason table from this in which has SeasonID and TeamID as foreign keys.</p>
  
  <p>In my controller I already have Season and Team and I just need to somehow represent relationship between them in my database.</p>
  
  <pre><code> [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;ActionResult&gt; AddTeam([Bind(Include = "SeasonId,SeasonYear,SeasonName,SelectedTeamId")]
          TeamSeasonViewModel teamSeasonVM)
      {
          Season season = await _seasonService.FindSeasonAsync(teamSeasonVM.SeasonId).ConfigureAwait(false);
          Team team = await _teamService.FindTeamAsync(teamSeasonVM.SelectedTeamId.Value).ConfigureAwait(false);
          return RedirectToAction("Index");
      }
  </code></pre>