title: ASP.NET MVC - Attaching an entity of type 'X' failed
body: <p>This is a model;</p>
  
  <pre><code>public class Urunler
  {
      public int UrunlerID { get; set; }
  
      public virtual Urunler Urun { get; set; }
  
      [NotMapped]
      public List&lt;int&gt; SelectedEtiketIds { get; set; }
  
      public virtual List&lt;UrunEtiketTablo&gt; Etiketler { get; set; }
  
  
  }
  </code></pre>
  
  <p>This is a code ;</p>
  
  <pre><code>if (model.SelectedEtiketIds != null)
                  {
                      if (model.Etiketler != null)
                      {
                          if (model.Etiketler.Count &gt; 0)
                          {
                              model.Etiketler.Where(i =&gt; !model.SelectedEtiketIds.Contains(i.UrunEtiket.EtiketID)).ToList().ForEach(i =&gt; model.Etiketler.Remove(i));
                              List&lt;int&gt; existlbl = model.Etiketler.Select(i =&gt; i.UrunEtiket.EtiketID).ToList();
                              db.Etikets.Where(i =&gt; model.SelectedEtiketIds.Except(existlbl).Contains(i.EtiketID)).ToList().ForEach(i =&gt; model.Etiketler.Add(new UrunEtiketTablo { UrunEtiket = i }));
                          }
                          else
                          {
                              db.Etikets.Where(i =&gt; model.SelectedEtiketIds.Contains(i.EtiketID)).ToList().ForEach(i =&gt; model.Etiketler.Add(new UrunEtiketTablo { UrunEtiket = i }));
                          }
                      }
                      else
                      {
                          model.Etiketler = db.Urunlers.Where(i =&gt; i.UrunlerID == model.UrunlerID).Select(i =&gt; i.Etiketler).FirstOrDefault();
                         db.Etikets.Where(i =&gt; model.SelectedEtiketIds.Contains(i.EtiketID)).ToList().ForEach(i =&gt; model.Etiketler.Add(new UrunEtiketTablo { UrunEtiket = i }));
                      }
                  }
                  else
                  {
                      if (model.Etiketler !=null &amp;&amp; model.Etiketler.Count &gt; 0)
                      {
                          model.Etiketler.Clear();
                      }
                  }
                     db.Entry(model).State = EntityState.Modified; //error line
  
                  }
                  db.SaveChanges();
                  return RedirectToAction("Urunler", "DaimiPanel");
  </code></pre>
  
  <p>When i add item to etiket list , i getting this error. </p>
  
  <blockquote>
    <p>Attaching an entity of type 'Tasarito.Models.UrunEtiketTablo' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.</p>
  </blockquote>
  
  <p>This is cross-table;</p>
  
  <pre><code>public class UrunEtiketTablo
      {
          public int UrunEtiketTabloID { get; set; }
  
          public virtual Etiket UrunEtiket { get; set; }
      }
  </code></pre>
  
  <p>Where can I make mistakes? ty.</p>