title: Ensure that explicitly set primary key values are unique
body: <p>I am getting following exception on my project:</p>
  
  <blockquote>
    <p>An exception of type 'System.InvalidOperationException' occurred in
    EntityFramework.dll but was not handled in user code</p>
    
    <p>Additional information: Saving or accepting changes failed because of
    more than one entity of type 'MyProject.Data.Poco.MyProjectCountry' 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.</p>
  </blockquote>
  
  <p>the error happens at the following line:</p>
  
  <pre><code>using (MyProjectDataContext context = new MyProjectDataContext())
  {
      MyProjectItemTag existingItemTag = (from p in context.ItemTags.Include(p =&gt; p.MyProjectGenre).Include(p =&gt; p.MyProjectCountry)
                                       where p.MyProjectUser.UserId == ItemTag.MyProjectUser.UserId &amp;&amp;
                                             p.MyProjectItem.ItemId == MyProjectItem.ItemId
                                       select p).FirstOrDefault();
  
  
      // new tag
      if (existingItemTag == null)
      {
          existingItemTag = ItemTag;
          existingItemTag.MyProjectItem.ItemId = MyProjectItem.ItemId;
      }
      // existing tag
      else
      {
          existingItemTag.MyProjectItem = new MyProjectItem { ItemId = MyProjectItem.ItemId };
          existingItemTag.MyProjectUser = new MyProjectUser { UserId = ItemTag.MyProjectUser.UserId };
      }
      // updates
      existingItemTag.MyProjectCountry = MyProjectCountry;
      if (MyProjectCountry != null)
      existingItemTag.MyProjectCountry = new MyProjectCountry()
      {
          MyProjectCountryId = MyProjectCountry.MyProjectCountryId
      };
      existingItemTag.MyProjectGenre = MyProjectGenre;
      context.Entry(existingItemTag.MyProjectItem).State = EntityState.Unchanged;
      context.Entry(existingItemTag.MyProjectUser).State = EntityState.Unchanged;
      context.Entry(existingItemTag.MyProjectCountry).State = EntityState.Unchanged;
      context.Entry(existingItemTag.MyProjectGenre).State = EntityState.Unchanged;
      if (existingItemTag.MyProjectCountry != null)
      {
          context.Entry(existingItemTag.MyProjectCountry).State = EntityState.Unchanged;
      }
      // db
      context.ItemTags.AddOrUpdate(existingItemTag);
      context.SaveChanges();
      return existingItemTag.ItemTagId;
  }
  
  </code></pre>
  
  <pre><code> context.Entry(existingItemTag.MyProjectCountry).State = EntityState.Unchanged;
  </code></pre>
  
  <p>My Class:</p>
  
  <pre><code>public class MyProjectItemTag
  {
      public int ItemTagId { get; set; }
      public MyProjectUser MyProjectUser { get; set; }
      public MyProjectItem MyProjectItem { get; set; }
      public MyProjectCountry MyProjectCountry { get; set; }
      public MyProjectGenre MyProjectGenre { get; set; }
      public MyProjectMood MyProjectMood { get; set; }
  
      public MyProjectItemTag()
      {
      }
  
      public MyProjectItemTag(string userId, string providerContentId)
      {
          MyProjectUser = new MyProjectUser
          {
              UserId = userId
          };
  
          MyProjectItem = new MyProjectItem
          {
              ProviderContentId = providerContentId
          };
      }
  }
  </code></pre>
  
  <p>My Config:</p>
  
  <pre><code>public class MyProjectItemTagConfiguration : EntityTypeConfiguration&lt;MyProjectItemTag&gt;
  {
      public MyProjectItemTagConfiguration()
      {
          ToTable("MyProjectItemTags");
          HasKey(p =&gt; p.ItemTagId);
          HasRequired(p =&gt; p.MyProjectUser);
          HasRequired(p =&gt; p.MyProjectItem);
          HasOptional(p =&gt; p.MyProjectCountry);
      }
  }
  </code></pre>
  
  <p>What I am missing here?</p>