views:

19

answers:

1

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();
    }
A: 

you should send the patientGUID to the method and then get the patient from the db and then do a delete

public List<Patient> DeletePatient(string patientGUID)
{

   var patient = DataBaseContent.Patients.SingleOrDefault(p => p.patientGUID == patientGUID);

    DataBaseContext.Patients.DeleteObject(patient);
    DataBaseContext.SaveChanges();
    return DataBaseContext.Patients.Include("PatientContacts").ToList();
}
Joakim
Why are we explicity iterating through the patiententity array to find the entity and deleting .//the below code is working public List<Patient> DeletePatient(Patient pat){var patient = DataBaseContext.Patients.SingleOrDefault(p => p.ID == pat.ID);DataBaseContext.Patients.DeleteObject(patient); since i have marked the pat as markas deleted and pat is a selftracking entity DeleteObject should do the look up and delete for me right , Just like update ......}
Somaraj
Since you now get the patient in that method it doesn't have the chance to loose its selftracking, which is why DeleteObject(patient); now works, so somewhere along the way you loose the selftracking in your code since it should have worked otherwise.
Joakim