Question

I use Entity Framework 4 and Self Tracking Entities. The schema is like:

Patient -> Examinations -> LeftPictures -> RightPictures

So there is TrackableCollection of these two relationships Patient 1 - * ....Pictures. Now when loading the customers Form and browsing the details I dont need to load these data images, only when another form is loaded for Examination details!

I am using a class library as a Data Repository to get data from the database (SQL Server) and this code:

public List<Patient> GetAllPatients()
{
    try
    {
        using (OptoEntities db = new OptoEntities())
        {
            List<Patient> list = db.Patients
                .Include("Addresses")
                .Include("PhoneNumbers")
                .Include("Examinations").ToList();

            list.ForEach(p =>
                             {
                                 p.ChangeTracker.ChangeTrackingEnabled = true;

                                 if (!p.Addresses.IsNull() &&
                                     p.Addresses.Count > 0)
                                     p.Addresses.ForEach(a => a.ChangeTracker.ChangeTrackingEnabled = true);

                                 if (!p.PhoneNumbers.IsNull() &&
                                     p.PhoneNumbers.Count > 0)
                                     p.PhoneNumbers.ForEach(a => a.ChangeTracker.ChangeTrackingEnabled = true);

                                 if (!p.Examinations.IsNull() &&
                                     p.Examinations.Count > 0)
                                     p.Examinations.ForEach(e =>
                                                                {
                                                                    e.ChangeTracker.ChangeTrackingEnabled = true;

                                                                });
                             });

            return list;
        }
    }
    catch (Exception ex)
    {
        return new List<Patient>();
    }
}

Now I need when calling the Examination details form to go and get all the Images for the Examination relationship (LeftEyePictures, RightEyePictures). I guess that is called Lazy Loading and I dont understood how to make it happen while I'm closing the Entities connection immidiately and I would like to stay like this.

I use BindingSource components through the application.

What is the best method to get the desired results?

Thank you.

Was it helpful?

Solution

Self tracking entities don't support lazy loading. Moreover lazy loading works only when entities are attached to live context. You don't need to close / dispose context immediately. In case of WinForms application context usually lives for longer time (you can follow one context per form or one context per presenter approach).

WinForms application is scenario for normal attached entities where all these features like lazy loading or change tracking work out of the box. STEs are supposed to be used in distributed systems where you need to serialize entity and pass it to another application (via web service call).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top