The scenario I am using LINQ is as follows:
I have a method that queries a table and returns one record using the following code
private L2SQLData.PatientFile getpatfile(long id)
{
var db = new HMSDataContext();
var patfile =
(from f in db.PatientFiles.Where(f=> f.Id == id) select f).FirstOrDefault() ;
return patfile;
}
Then another code calls the method above and takes object/record that was returned. Then deletes it from the same table as follows:
L2SQLData.PatientFile patfile = getpatfile(long.Parse(id));
var db = new HMSDataContext();
db.PatientFiles.DeleteOnSubmit(patfile);
db.SubmitChanges();
On running it, VS2010 screams with the error: Cannot remove an entity that has not been attached. What am I doing wrong? Anyone?
Try
db.PatientFiles.Attach(patfile)or try using only one globaly HMSDataContext