I have a service which will add , update and delete a patient entity . I made the patient entity is selftracking entity .
But only for delete i need to call 'Attach' the entity before deleting , why not attach required for add and update .
What is the logic behind attaching only for delete ( I have cascading property = true)
public List<Patient> AddPatient(Patient pat)
{
DataBaseContext.Patients.AddObject(pat);
DataBaseContext.SaveChanges();
return DataBaseContext.Patients.Include("PatientContacts").ToList();
}
public List<Patient> DeletePatient(Patient pat)
{
//Why only for delete we need to attach ??????????
DataBaseContext.Patients.Attach(pat);
DataBaseContext.Patients.DeleteObject(pat);
DataBaseContext.SaveChanges();
return DataBaseContext.Patients.Include("PatientContacts").ToList();
}
public List<Patient> UpdatePatient(Patient pat)
{
DataBaseContext.Patients.ApplyChanges(pat);
DataBaseContext.SaveChanges();
return DataBaseContext.Patients.Include("PatientContacts").ToList();
}