title: MVC EF Adding object to database
body: <p>So this is a continuation of the question <a href="https://stackoverflow.com/questions/37062540/entityframework-creating-many-to-many-relationships-automatically/37062598?noredirect=1#comment61672305_37062598">here</a></p>
  
  <p>I am trying this below:</p>
  
  <pre><code>public DbSet&lt;User&gt; Users { get; set; }
  public DbSet&lt;Sport&gt; Sports { get; set; }
  public DbSet&lt;Team&gt; Teams { get; set; }
  </code></pre>
  
  <ul>
  <li>I can have many Sports</li>
  <li>I can have many Teams in a Sport</li>
  <li>I can have many Teams in a User</li>
  <li>I can have many Users in a Team</li>
  </ul>
  
  <p><strong>Sport</strong></p>
  
  <pre><code>public int ID { get; set; }
  public List&lt;Team&gt; Teams { get; set; }
  </code></pre>
  
  <p><strong>Team</strong></p>
  
  <pre><code>public Team(int ID, int SportID, int Wins, int Losses, String Name, String Description, double Reputation, List&lt;User&gt; Members, byte[] TeamLogo)
   {
       this.ID = ID;
       this.SportID = SportID;
       this.Wins = Wins;
       this.Losses = Losses;
       this.Name = Name;
       this.Description = Description;
       this.Reputation = Reputation;
       this.Members = Members;
       this.TeamLogo = TeamLogo;
   }
  public int ID { get; set; }
  public List&lt;User&gt; Members { get; set; }
  public int SportID { get; set; }
  </code></pre>
  
  <p><strong>User</strong></p>
  
  <pre><code>public int UserID { get; set; }
  public List&lt;Team&gt; Teams { get; set; }
  </code></pre>
  
  <p><strong>DB</strong></p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          // Adds middle table between Users and Teams
          modelBuilder.Entity&lt;User&gt;()
              .HasMany&lt;Team&gt;(t =&gt; t.Teams)
              .WithMany(m =&gt; m.Members)
              .Map(w =&gt; w.ToTable("UserTeam")
                         .MapLeftKey("UserID")
                         .MapRightKey("TeamID"));
      }
  
  public void addTeam(Team team, String sport)
      {
          // Add the team to the correct sport
          Teams.Add(team);
          this.SaveChanges();
      }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>model.NewTeam = new Team(0, db.getSportID(model.SelectedSport), 0, 0, model.TeamName, model.TeamDescription,
                     100, new List&lt;User&gt;() { model.currentUser }, model.ActualImage);
  db.addTeam(model.NewTeam, model.SelectedSport);
  </code></pre>
  
  <p>Issue:</p>
  
  <p>It adds a record to the Teams table just fine. It adds the record to the UserTeam table just fine. The problem is that it is also adding a new record to the User table as well so I have repeating users in my User table. Also, when I query the Teams Table, I get a null List Members so EF isn't properly getting the Members of each team from the Many-Many table.</p>