Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

asp.net core - The instance of entity type 'x' cannot be tracked because another instance with the same key value for {'a', 'b'} is already being tracked

I got the error in the title while editing relational table between many-to-many relationship. It will not let duplicate on table, so I try to remove rows and then create new one but it didn't work.

public void Update(ThermoformProduct entity, int[] thermoformCategoryIds)
{
    using (var context = new ShopContext())
    {
        var product = context.ThermoformProducts                  
            .Include(i => i.ThermoformProductCategories)                
            .FirstOrDefault(i => i.ProductId == entity.ProductId);            

        if (product != null)
        {           
            product.Code = entity.Code;
            product.Culture = entity.Culture;
            product.Renk = entity.Renk;
            product.UstGenislik = entity.UstGenislik;
            product.UstCap = entity.UstCap;
            product.AltCap = entity.AltCap;
            product.TbCap = entity.TbCap;
            product.Yukseklik = entity.Yukseklik;
            product.Hacim = entity.Hacim;
            product.TamHacim = entity.TamHacim;
            product.Baski = entity.Baski;
            product.SosisIciAdet = entity.SosisIciAdet;
            product.KoliIciAdet = entity.KoliIciAdet;
            product.ImageUrl = entity.ImageUrl;

            product.ThermoformProductCategories.RemoveAll(s=>s.ProductId == product.ProductId);

            product.ThermoformProductCategories = thermoformCategoryIds.Select(catid =>  new ThermoformProductCategory()
            {
                ProductId = product.ProductId,
                ThermoformProduct = product,
                CategoryId = catid,
                ThermoformCategory = context.ThermoformCategories.Where(i => i.CategoryId == catid).FirstOrDefault()

            }).ToList();

            context.SaveChanges();
        }
    }
}
question from:https://stackoverflow.com/questions/65870800/the-instance-of-entity-type-x-cannot-be-tracked-because-another-instance-with

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

EF cannot track two different instance of an entity type with same primary key. You have included the related ThermoformProductCategory entities, so they are being tracked by the context. When you remove them, they are cleared from the ThermoformProductCategories property of that product, but they are not removed from the context, and are still being tracked. Finally when you create the new list of ThermoformProductCategory, some of the new ones' primary key are matching the previous ones' (which already exist in the context)

Since you are creating the entire list again, you don't need to fetch the related entities in the first place. Simply assign a new list and EF will replace the entire list of related entities -

var product = context.ThermoformProducts.FirstOrDefault(i => i.ProductId == entity.ProductId);
if (product != null)
{       
    // set all the properties

    product.ThermoformProductCategories = thermoformCategoryIds.Select(catid =>  new ThermoformProductCategory()
    {
        ProductId = product.ProductId,
        CategoryId = catid
    }).ToList();

    context.SaveChanges();     
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...