I am attempting to remove an object from my sql database. I have been using this same method in multiple places and they all work fine except for here.
My Code:
foreach (TestCase testCase in testCaseList)
{
db.TestCases.Remove(testCase);
await db.SaveChangesAsync();
}
The error that I get from this code is: "The object cannot be deleted because it was not found in the ObjectStateManager"
I found a solution to this problem which was adding an attach. The resulting code was:
foreach (TestCase testCase in testCaseList)
{
db.TestCases.Attach(testCase);
db.TestCases.Remove(testCase);
await db.SaveChangesAsync();
{
With this code, I get the following error: "Store update, insert, or delete statement affected an unexpected number of rows(0)." I found some information on this error, but nothing that seemed to apply to my issue. Is there a specific reason these issues are arising and what can I do to solve the errors?