0

This is a model;

public class Urunler
{
    public int UrunlerID { get; set; }

    public virtual Urunler Urun { get; set; }

    [NotMapped]
    public List<int> SelectedEtiketIds { get; set; }

    public virtual List<UrunEtiketTablo> Etiketler { get; set; }


}

This is a code ;

if (model.SelectedEtiketIds != null)
                {
                    if (model.Etiketler != null)
                    {
                        if (model.Etiketler.Count > 0)
                        {
                            model.Etiketler.Where(i => !model.SelectedEtiketIds.Contains(i.UrunEtiket.EtiketID)).ToList().ForEach(i => model.Etiketler.Remove(i));
                            List<int> existlbl = model.Etiketler.Select(i => i.UrunEtiket.EtiketID).ToList();
                            db.Etikets.Where(i => model.SelectedEtiketIds.Except(existlbl).Contains(i.EtiketID)).ToList().ForEach(i => model.Etiketler.Add(new UrunEtiketTablo { UrunEtiket = i }));
                        }
                        else
                        {
                            db.Etikets.Where(i => model.SelectedEtiketIds.Contains(i.EtiketID)).ToList().ForEach(i => model.Etiketler.Add(new UrunEtiketTablo { UrunEtiket = i }));
                        }
                    }
                    else
                    {
                        model.Etiketler = db.Urunlers.Where(i => i.UrunlerID == model.UrunlerID).Select(i => i.Etiketler).FirstOrDefault();
                       db.Etikets.Where(i => model.SelectedEtiketIds.Contains(i.EtiketID)).ToList().ForEach(i => model.Etiketler.Add(new UrunEtiketTablo { UrunEtiket = i }));
                    }
                }
                else
                {
                    if (model.Etiketler !=null && model.Etiketler.Count > 0)
                    {
                        model.Etiketler.Clear();
                    }
                }
                   db.Entry(model).State = EntityState.Modified; //error line

                }
                db.SaveChanges();
                return RedirectToAction("Urunler", "DaimiPanel");

When i add item to etiket list , i getting this error.

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.

This is cross-table;

public class UrunEtiketTablo
    {
        public int UrunEtiketTabloID { get; set; }

        public virtual Etiket UrunEtiket { get; set; }
    }

Where can I make mistakes? ty.

Serkan
  • 25
  • 1
  • 10
  • Possible duplicate. See this question: http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent – Sabuncu Mar 25 '16 at 19:28
  • ı look this link. But not same problem. My model creating in action with "SelectedEtiketIds" he get from view. – Serkan Mar 25 '16 at 19:44
  • Please have a look at my answer on [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent/39557606#39557606). – Murat Yıldız Sep 18 '16 at 12:26

1 Answers1

1

I found the same problem and in my case, I just change the code

db.Entry(model).State = EntityState.Modified; //error line

into

db.Set<YourModel>().AddOrUpdate(model);

and it works fine. Hopefully it can works in your problem as well.

Sambalado
  • 33
  • 7