title: Why am I getting duplicate of entities when I updating an existing entity?
body: <p>I save some data to my database and Im using code first. Everything works fine until I want to change an entity. For example: I add four persons to the database, and when I updating one of them the database adds duplicates of all persons. </p>
  
  <p><strong>UPDATE</strong>
  The problem will be in this code:</p>
  
  <pre><code>Conversation newConv = new Conversation { Name = chatPerson.Name };
          newConv.Members.Add(person);
          newConv.Members.Add(chatPerson);
  </code></pre>
  
  <p><code>Conversation</code> has a list of <code>Members</code> and when Im trying to add <code>Members</code> to the list then I will get my duplicate in the database.</p>
  
  <p><strong>OLD POST</strong></p>
  
  <p>The first thing I do is to load one person:</p>
  
  <pre><code>var person = repo.GetWholePerson(3);
  </code></pre>
  
  <p>Code that returns the person:</p>
  
  <pre><code>public Person GetWholePerson(int id)
      {
          return _ctx.Persons
              .Include(a =&gt; a.Colleagues)
              .Include(a =&gt; a.Conversations)
              .FirstOrDefault(p =&gt; p.Id == id);
      }
  </code></pre>
  
  <p>Here is the code that saves to database:</p>
  
  <pre><code>using (Repository&lt;Person&gt; repo = new Repository&lt;Person&gt;())
      {
         var per = repo.Get(person.Id);
         var chatPer = repo.Get(chatPerson.Id);
  
         per.Conversations.Add(newConv);
         chatPer.Conversations.Add(newConv);
  
         repo.Update(per);
         repo.Update(chatPer);
      }
  </code></pre>
  
  <p>And here is the code in the Repository:</p>
  
  <pre><code>public void Update(T entity)
      {
          dbSet.Attach(entity);
          _ctx.Entry(entity).State = EntityState.Modified;
          _ctx.SaveChanges();
      }
  </code></pre>
  
  <p>Person class:</p>
  
  <pre><code>public class Person
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public byte[] Image { get; set; }
  
      public virtual ICollection&lt;Conversation&gt; Conversations { get; set; }
  
      public virtual ICollection&lt;Person&gt; Colleagues { get; set; }
      public virtual ICollection&lt;Person&gt; Test { get; set; }
  
  
      public Person()
      {
          Conversations = new List&lt;Conversation&gt;();
          Colleagues = new List&lt;Person&gt;();
          Test = new List&lt;Person&gt;();
      }
  }
  </code></pre>
  
  <p>Conversation class:</p>
  
  <pre><code>public class Conversation
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;Message&gt; Messages { get; set; }
      public virtual ICollection&lt;Person&gt; Members { get; set; }
  
      public Conversation()
      {
          Messages = new List&lt;Message&gt;();
          Members = new List&lt;Person&gt;();
      }
  }
  </code></pre>
  
  <p>Everything works fine! But when I connecting to my database there are four new persons. =(</p>