title: An entity with the same identity already exists in this EntitySet. - WCF RIA Services
body: <p>I have the following code that I would expect would check to see if an entity exists and, if not, add it to the collection. I then call submit changes on the context to save the changes.</p>
  
  <pre><code>int kwID = (int)(from d in this._keywordSource.Source where d.keyword.ToLower() == searchText.ToLower() select d.keywordID).FirstOrDefault();
  if ( null == this._context.Rules.Where(e =&gt; e.keywordID == kwID &amp;&amp; e.searchTerm == item.SearchTerm).FirstOrDefault())
      {
          this._context.Rules.Add(new Rule() { keywordID = kwID, searchTerm = item.SearchTerm, processed = false });
      }
  </code></pre>
  
  <p>I am testing this with 3 keywords/searchTerm combos:</p>
  
  <p>Apple/Apple</p>
  
  <p>iPad/iPad</p>
  
  <p>iPod/iPod</p>
  
  <p>The first attempt never runs the .Add line of code. The second does, successfully. The third throws the error <code>An entity with the same identity already exists in this EntitySet.</code>.</p>
  
  <p>I've read that this has something to do with the identity &amp; seed, especially if your seed is 0 in the db. Mine isn't (set to 1). The EntitySet itself has ~1900 items in it. </p>
  
  <p>What am I missing here?</p>
  
  <p>[EDIT]
  Added the kwID code to show that keyword is ultimately the kwID in the conditional. </p>
  
  <p>The primaryKey of this table is ruleID. This is what I believe is being violated in the error message.... but I don't know how or why. </p>