title: EF 'Cannot insert duplicate key in object' when updating an existing object in a repository
body: <p>I fairly inexperienced with EF, and I have been struggling with what I am sure is a rather trivial issue, for quite a while.
  I have two tables, SpecialCondition and SpecialConditionDepartment. A special condition can apply to several departments, so in the SpecialConditionDepartment table I store the conditionID and the deparmentID. If a condition applies to two departments, there will be two rows in the SpecialConditionDepartment table, with the same conditionID.</p>
  
  <pre><code>public class SpecialCondition
  {
      public int ID { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;SpecialConditionDepartment&gt; Departments { get; set; }
  }
  
   public class SpecialConditionDepartment
  {
      private SpecialConditionDepartment()
      {
      }
  
      public SpecialConditionDepartment(int conditionID, int departmentID)
      {
          ConditionID = conditionID;
          DepartmentID = departmentID;
      }
  
      public int ConditionID { get; set; }
      public int DepartmentID { get; set; }
  }
  
  
   var specialCondition =modelBuilder.Entity&lt;SpecialCondition&gt;().ToTable("SpecialCondition");
          specialCondition.HasMany(o =&gt; o.Departments)
              .WithOptional()
              .HasForeignKey(c =&gt; c.ConditionID);
  
          modelBuilder.Entity&lt;SpecialConditionDepartment&gt;().ToTable("SpecialConditionDepartment")
              .HasKey(c =&gt; new { c.ConditionID, c.DepartmentID });
  </code></pre>
  
  <p>And finally the update method, which is inside a web service, is fairly simple. I grab the object from the DB, update name and Departments list and then update using the generic repository Update():</p>
  
  <pre><code> public SpecialConditionDto UpdateCondition(SpecialConditionDto dto)
   {
          SpecialCondition specialcondition = specialConditionRepository.GetById(dto.ID);
  
          specialcondition.Name = dto.Name;
          specialcondition.Departments =
              mapper.Map&lt;ICollection&lt;SpecialConditionDepartmentDto&gt;, ICollection&lt;SpecialConditionDepartment&gt;&gt;(
                  dto.Departments);
           specialConditionRepository.Update(specialcondition);
           unitOfWork.Commit();
           return specialcondition.Map&lt;SpecialConditionDto&gt;();
  }
  </code></pre>
  
  <p>The SpecialConditionRepository inherits from the RepositoryBase, which in turn inherits from IRepository</p>
  
  <pre><code>public class SpecialConditionRepository : RepositoryBase&lt;SpecialCondition&gt;, ISpecialConditionRepository
  public abstract class RepositoryBase&lt;T&gt; : IRepository&lt;T&gt; where T : class
  </code></pre>
  
  <p>And the repository base, where all the generic Add, GetById, Update and so on are defined</p>
  
  <pre><code>public abstract class RepositoryBase&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
    protected IDbSet&lt;T&gt; Set
    {
      get { return Context.Set&lt;T&gt;(); }
    }
  
    protected RepositoryBase()
    {
      DatabaseFactory = new DatabaseFactory();
    }
    private EFContext _context;
    public EFContext Context
    {
      get { return _context ?? (Context = DatabaseFactory.Create()); }
      set { _context = value; }
    }
  }
  </code></pre>
  
  <p>In the context is where the models and IDBSets are defined</p>
  
  <pre><code>public IDbSet&lt;SpecialCondition&gt; SpecialConditions { get; set; }
  public IDbSet&lt;SpecialConditionDepartment&gt; SpecialConditionDepartments { get; set; }
  </code></pre>
  
  <p>The same code works for creating a special condition. When trying to update however, it does not understand that I want it to update the departments as well, and it tries to add new rows to the SpecialConditionDepartment table, which ends up crashing my code. </p>
  
  <p>What I am missing?</p>