title: Entity Framework 4. Join Tables. Entity Framework perfoming an Insert when no action is required
body: <p>I have a database setup with the following: -  </p>
  
  <pre><code>Person Table  
  Hobby Table
  Game Table  
  GameInfo Table  
  </code></pre>
  
  <p>Person [1 - M] Hobby [1 - M] Game [M - 1] GameInfo</p>
  
  <p><code>Game</code> is just a join from <code>Hobby</code> to <code>GameInfo</code></p>
  
  <p>I am having an issue whereby I would fetch <code>Person</code> which has a <code>Collection&lt;Game&gt;</code> and add to this collection (i.e. I am just updating links, not wanting to insert new <code>GameInfo</code>).</p>
  
  <p>Upon Calling <code>SaveChanges()</code> EntityFramework will insert the links AS WELL AS inserting new <code>GameInfo</code>, which is not my desired result.</p>
  
  <p>I have looked at Entry().State etc but the problem is where I am handling the updates of my <code>Person</code> is outside of the context.</p>
  
  <p>I am basically fetching a <code>Person</code> creating a new <code>Game</code> with Ids that I know exist already and then calling the <code>SaveChanges()</code> and would expect that it would just insert into the <code>Game</code> Table, not the GameInfo table</p>
  
  <p>EDIT 1: Code Sample - sort of</p>
  
  <pre><code>public void Save(Profile profile)
  {
      using (GDContext context = GetContext())
      {
          DataProfile dataProfile = context.Profiles.Single(u =&gt; u.ProfileId == profile.Id);
          ProfileHandler.HandleDataModelChanges(dataProfile, profile);
          context.SaveChanges();
      }
  }
  
  public override void HandleDataModelChanges(DataProfile dataModel, Profile model)
  {
      dataModel.ProfileId = model.Id;
      dataModel.FirstName = model.FirstName;
      dataModel.LastName = model.LastName;
      dataModel.DateOfBirth = model.DateOfBirth;
      dataModel.Email = model.Email;
  
      foreach(var hobby in model.Hobbies)
      {
          DataHobby dataHobby = dataModel.Hobbies.SingleOrDefault(p =&gt; p.HobbyId == hobby.HobbyId);
  
          if (dataHobby == null)
          {
              dataHobby = new DataHobby();
          }
  
          HobbyHandler.HandleDataModelChanges(dataHobby, hobby);
      }
  }
  
  public override void HandleDataModelChanges(DataHobby dataModel, Hobby model)
  {
      dataModel.HobbyId = model.Id;
  
      HandleGames(dataModel, model);
  
      HandleCrafts(dataModel, model);
  
      HandleCollections(dataModel, model);
  }
  
  private void HandleGames(DataHobby dataModel, Hobby model)
  {
      IEnumerable&lt;DataGame&gt; gamesToRemove = dataModel.Games.Where(g =&gt; !model.Games.Any(ds =&gt; ds.Id == g.GameId)).ToArray();
  
      foreach (var game in gamesToRemove)
      {
          dataModel.Games.Remove(game);
      }
  
      foreach (var game in model.Games)
      {
          if (!dataModel.Games.Any(e =&gt; e.GameId == game.Id))
          {
              DataGame dataGame = new DataGame();
              dataGame.GameId = game.Id;
              dataGame.GameName = game.Name;
  
              dataModel.Games.Add(dataGame);
          }
      }
  }
  </code></pre>
  
  <p>EDIT 2 - Context configuration</p>
  
  <pre><code>this.Configuration.LazyLoadingEnabled = false;
  this.Configuration.AutoDetectChangesEnabled = true;
  
  public GameInfoConfiguration()
  {
      HasKey(x =&gt; x.GameId);
      ToTable("GameData");
  }
  
  public PersonConfiguration()
  {
      HasKey(x =&gt; x.PersonId);
  
      ToTable("Person");
  }
  
  public HobbyConfiguration()
  {
      HasKey(x =&gt; x.HobbyId);
  
      HasRequired(x =&gt; x.Person).WithMany(x =&gt; x.Hobbies);
  
      HasMany(x =&gt; x.Games).WithMany(g =&gt; g.Hobbies).Map(x =&gt; x.MapLeftKey("HobbieId").MapRightKey("GameId").ToTable("PersonGame"));
  
      ToTable("HobbyGame");
  }
  </code></pre>