title: Entity framework, InvalidOperationException when removing and adding object twice
body: <p>I'm using Entity Framework 6, database first. </p>
  
  <p>I have a database table called SubstanceNumbers and a datagrid together with a textbox and some other gui elements for adding new entries, the SubstanceNumber have a foreign key field to a Substance. </p>
  
  <p>The scenario that gives me a crash is when I add a SubstanceSubstance, delete it, add it and delete it again, not the most common use case, but I feel like it reveals some larger mistake I've done. </p>
  
  <pre><code>My error message: System.InvalidOperationException was unhandled
  
  Message=Saving or accepting changes failed because more than one entity of type 'SHMD_Edit.Models.EF.SubstanceNumber' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.
  </code></pre>
  
  <p>When I add the SubstanceNumbers, I don't want to set the primary key field SubstanceNumberId, because the database should handle the auto-incrementing, so that will be 0 until I actually context.SaveChanges().</p>
  
  <p>My add/delete commands look like this: </p>
  
  <pre><code>public RelayCommand AddSubstanceNumberCommand
      {
          get
          {
              return new RelayCommand(
                  (x) =&gt;
                  {
                      var number = new SubstanceNumber();
                      number.SubstanceID = Id;
                      number.ModifiedBy = Main.op.LoginName;
                      number.ModifiedDate = DateTime.Now;
                      number.CreatedDate = DateTime.Now;
                      number.Type = Converters.NumberTypeToStringConverter.translation.First(t =&gt; t.Value == SelectedNewSubstanceNumberType).Key;
                      number.NumberText = Validator.Normalize(NewSubstanceNumber, SelectedNewSubstanceNumberType);
                      var temp = NewSubstanceNumber.Replace("-", "");
                      number.NumberValue = Int32.Parse(temp.Substring(0, temp.Length - 1));
                      if (NewSubstanceNumber != null &amp;&amp; !SubstanceNumbers.Any(sn =&gt; sn.NumberText.Replace(" ", "") == number.NumberText &amp;&amp; sn.Type == number.Type))
                          SubstanceNumbers.Add(number);
  
                      NewSubstanceNumber = "";
  
                  }, param =&gt; this.CanAddSubstanceNumber);
          }
      }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public RelayCommand RemoveSubstanceNumberCommand
      {
          get
          {
              return new RelayCommand(
                  (x) =&gt;
                  {
                      if (SelectedSubstanceNumber != null)
                      {
                          context.Entry(SelectedSubstanceNumber).State = EntityState.Deleted;
                          SubstanceNumbers.Remove(SelectedSubstanceNumber);
                      }
                  });
          }
      }
  </code></pre>
  
  <p>My table: </p>
  
  <pre><code>Column name, Data Type, Allow Nulls (is identity, identity increment=1)
  SubstanceNumberID   int Unchecked
  SubstanceID int Unchecked
  Type    int Unchecked
  NumberText  char(16)    Unchecked
  NumberValue int Unchecked
  CreatedDate datetime    Unchecked
  ModifiedDate    datetime    Unchecked
  ModifiedBy  nvarchar(16)    Unchecked
  RowVersion  timestamp   Unchecked
  </code></pre>
  
  <p>So I assume there is some synchronization error between the context and my list of SubstanceNumbers, but I can't see what it is. Can you point it out?</p>