locked
Not able to Create and update Database data in the same action RRS feed

  • Question

  • User1750806012 posted

    I am trying to create a new and update in 2 different database tables

    controller:

     Products product = new Products()
                    {
                        Id = NewId,
                        Title = Title,
                        Description = Description,
                    };
                    _context.Product.Add(product);
                    await _context.SaveChangesAsync();
    
                var FindCatergory = _context.Categorys.FirstOrDefaultAsync(x => x.category == Category);
                int Categorycount = FindCatergory.Result.LinkedProducts;
    
                var updated = new Categorys {CategoryId = FindCatergory.Result.CategoryId};
                updated.LinkedProducts = Categorycount + 1;
                _context.Categorys.Update(updated);
                await _context.SaveChangesAsync();

    I keep getting the same error no matter what i try:

    InvalidOperationException: The instance of entity type 'Categorys' cannot be tracked because another instance with the same key value for {'CategoryId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.




    Thursday, April 15, 2021 10:28 PM

Answers

  • User1686398519 posted

    Hi osyris, 

    var FindCatergory = _context.Categorys.FirstOrDefaultAsync(x => x.category == Category);

    FindCatergory is obtained by the context, so the context immediately starts tracking it.

    1. In other words, when you change the property value of the tracked entity (FindCatergory), the context will change the entity's EntityState to Modified, and ChangeTracker will record the old property value and the new property value.
    2. So you can modify your code to:
      • var FindCatergory = _context.Categorys.FirstOrDefaultAsync(x => x.category == Category);
        int Categorycount = FindCatergory.Result.LinkedProducts;
        FindCatergory.LinkedProducts = Categorycount + 1;
        await _context.SaveChangesAsync();

    Modifying data via the DbContext

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 16, 2021 4:06 AM

All replies

  • User1686398519 posted

    Hi osyris, 

    var FindCatergory = _context.Categorys.FirstOrDefaultAsync(x => x.category == Category);

    FindCatergory is obtained by the context, so the context immediately starts tracking it.

    1. In other words, when you change the property value of the tracked entity (FindCatergory), the context will change the entity's EntityState to Modified, and ChangeTracker will record the old property value and the new property value.
    2. So you can modify your code to:
      • var FindCatergory = _context.Categorys.FirstOrDefaultAsync(x => x.category == Category);
        int Categorycount = FindCatergory.Result.LinkedProducts;
        FindCatergory.LinkedProducts = Categorycount + 1;
        await _context.SaveChangesAsync();

    Modifying data via the DbContext

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 16, 2021 4:06 AM
  • User1750806012 posted

    Thank you very much.

    Friday, April 16, 2021 11:30 AM