0
using (var db = new MyDbContext())
{    
    var removeFromDb = db.StockQuantities.Take(stockToRemoveCount).ToList();

    for (int i = 0; i < removeFromDb.Count; i++)
    {
        db.Entry(removeFromDb[i]).State = EntityState.Deleted;
        db.Remove(removeFromDb[i]);

        stocktakes.Add(stockTake);
        stockTake.CurrentQty = stockTake.CapturedQty;

        db.Stocktakes.Update(stockTake);
    }

    db.SaveChanges();
}

I am not sure what I am doing wrong here, I have been stuck here for a long time now and I find errors or do not throw any errors when running the code, but it's not removing entities but adding works fine.

  • 1
    For a simple remove / delete just use the DbSet.RemoveRange: `var removeFromDb = db.StockQuantities.Take(stockToRemoveCount).ToList(); db.StockQuantities.RemoveRange(removeFromDb); db.SaveChanges();` If you want additional help you need to create an [mcve] and also explain *what* is actually occurring because "I have been stuck here a long time" is not descriptive of the actual problem. – Igor Aug 15 '22 at 20:31
  • Let's try this, but I think I have tried this, funny enough I am able to add without range but looping through the list. – Lonwabo Msingelwa Aug 15 '22 at 20:35
  • @Igor nothing happens I have been debugging and stepping over checking if there's any errors. – Lonwabo Msingelwa Aug 15 '22 at 20:36
  • The code after `Remove` looks suspicious. What is `stockTake`, where it comes from, what does it contain and does it have relationship with `StockQuantity`? It's possible that `db.Stocktakes.Update(stockTake);` negates the remove operation in case the `stockTake` object contains references to the entities you are trying to delete. We need to see the whole picture in order to help. – Ivan Stoev Aug 16 '22 at 05:12
  • There is no relation between stockTake and StockQuantity, but I have noticed that if the records are more than 10 000 it does not remove but if less it works. It's weird hey. – Lonwabo Msingelwa Aug 16 '22 at 08:46
  • @LonwaboMsingelwa That's quite important detail. If I recall correctly, 10 000 is the SqlServer limit for number of parameters in a SQL command. You might be hitting some EF Core implementation bug. – Ivan Stoev Aug 17 '22 at 07:43

1 Answers1

0

you can choose between three options. The first is to just remove the entity from the DbSet

using (var db = new MyDbContext())
{     
    var removeFromDb = 
       db.StockQuantities.Take(stockToRemoveCount).ToList();

   for (int i = 0; i < removeFromDb.Count; i++)
   {
       db.StockQuantities.Remove(removeFromDb[i]);
   }

   stockTake.CurrentQty = stockTake.CapturedQty;
   db.Stocktakes.Add(stockTake);
   db.SaveChanges();
}

The second option would be to optimize the code using RemoveRange method. This should be the recommended approach since it will run DetectChanges on the dbSet only once, while the first approach will trigger DetectChange on each iteration.

using (var db = new MyDbContext())
{     
    var removeFromDb = 
       db.StockQuantities.Take(stockToRemoveCount).ToList();
   
   db.StockQuantities.RemoveRange(removeFromDb);
   
   stockTake.CurrentQty = stockTake.CapturedQty;

   db.Stocktakes.Add(stockTake);
   
   db.SaveChanges();
}

And the third option would be to mark the entity state as deleted

using (var db = new MyDbContext())
{     
    var removeFromDb = 
       db.StockQuantities.Take(stockToRemoveCount).ToList();

   for (int i = 0; i < removeFromDb.Count; i++)
   {
       
       db.Entry(removeFromDb[i]).State = EntityState.Deleted;    
   }

   stockTake.CurrentQty = stockTake.CapturedQty;

   db.Entry(stockTake).State = EntityState.Added;
   db.SaveChanges();
}
Anton Kovachev
  • 322
  • 3
  • 6
  • I changed the data and all of sudden it worked, now I will have to check why. Thank you. Because I had tried exactly what you have shared. – Lonwabo Msingelwa Aug 16 '22 at 06:08