title: Inserting new record into linking table with linq to entities with POCO
body: <p>I have a Team table and a Player table in many to many relationship. There is a linking table called TeamOnPlayer. EF with POCO generates navigation propertie called Person for the Team entity and also generates a nav. prop. called Team for the People entity.</p>
  
  <p>I'm trying to insert a new record into the TeamOnPlayer table, but EF and POCO hides it. I tried to do this:</p>
  
  <pre><code>public static void AddPersonToTeam(int TeamId, int PersonId)
      {
          using (var ef = new korfballReportEntities())
          {
              var team = GetTeam(TeamId);
              var person = GetPerson(PersonId);
  
              team.Person.Add(person);
              person.Team.Add(team);
  
              ef.SaveChanges();
          }
      }
  </code></pre>
  
  <p>The GetTeam(TeamId) and GetPerson(PersonId) gets the right team and person:</p>
  
  <pre><code>public static Team GetTeam(int id)
      {
          using (var ef = new korfballReportEntities())
          {
              var q = from l in ef.Team
                      where l.Id == id
                      select l;
              return q.Single();
          }
      }
  public static Person GetPerson(int id)
      {
          using (var ef = new korfballReportEntities())
          {
              var query = from p in ef.Person
                          where p.Id == id
                          select p;
              return query.Single();
          }
      }
  </code></pre>
  
  <p>When  it tries to call the team.Person.Add(person) it throws an exception:</p>
  
  <blockquote>
    <p>"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."    System.Exception {System.ObjectDisposedException}</p>
  </blockquote>
  
  <p>Can anyone please show me the correct way? </p>
  
  <p><strong>Edit</strong></p>
  
  <p>Now I understand what the problem was, thanks to you. I was a bit confused about the using blocks you included. For example this:</p>
  
  <pre><code>using (var ef = new korfballReportEntities())
  {
  //switch lazy loading off, only in this single context
  ef.Configuration.LazyLoadingEnabled = false;
  
  var repository = new MyRepository(ef);
  repository.AddPersonToTeam(int TeamId, int PersonId);
  }
  </code></pre>
  
  <p>Where should I put it?</p>
  
  <p>I've done something else. I simply did this, and it worked fine.</p>
  
  <pre><code>public static void AddPersonToTeam(int TeamId, int PersonId)
  {
      using (var ef = new korfballReportEntities())
      {
          var q = from t in ef.Team
                  where t.Id == TeamId
                  select t;
          var team =  q.Single();
  
          var q2 = from p in ef.Person
                  where p.Id == PersonId
                  select p;
          var person = q2.Single();
  
          try
          {
              team.Person.Add(person);
              person.Team.Add(team);
          }
          catch (Exception e)
          {  
          }
  
          ef.SaveChanges();
      }
  }
  </code></pre>
  
  <p>The only problem is, that i coludn't reuse my GetPerson(int id) and GetTeam(int id) method.</p>
  
  <p>What do you think? Is it okay? Is this an ugly way?</p>