I'm getting the error Cannot remove an entity that has not been attached.
when I try to delete a record. I've googled around and although there's plenty of places to find the solution to fix this problem, the suggested fix doesn't get me further:
using (MyDataContext TheDC = new MyDataContext())
{
TheDC.MyTable.Attach(ARecord); //added this line but doesn't fix it.
TheDC.MyTable.DeleteOnSubmit(ARecord);
TheDC.SubmitChanges();
My bigger question is this: does this issue ONLY affect delete queries or does it affect other kinds of queries too and I just haven't bumped into this problem yet. I've written an update query and it seems to work without running into this error.
Thanks.
Please refer to this SO Post: How to delete in linq to sql?
For me, the fix was just searching for the database object first.
In my mind, I could only delete what was already there, so I needed to go get the items in the database that I wanted to delete first. This also works with collections and DeleteAllOnSubmit().