Say the EF6 code first is used. Say we have a simple two table relationship. Where a patient has one doctor associated.
class Patient
{
[Key]
public int id { get; set; }
public string Name { get; set; }
[ForeignKey("Doctor")]
public int DoctorId { get; set; }
public virtual MedicalPersonel Doctor { get; set; }
}
class MedicalPersonel
{
[Key]
public int id { get; set; }
public string Name { get; set; }
}
There is a function Include(string path) to load the Doctor, when I am loading the patient. But what would be the opposite of include if I don't want to load the Doctor if the doctor contains some big fields like images?
Thanks!
That's the fun part about lazy loading, you won't load the Doctor
unless you access the property. To be entirely sure you don't 'accidentally' lazy load anything you can
Remove the virtual
keyword all together from the properties you never want lazily loaded.
When you create your context, disable lazy loading for that instance of the DbContext
:
myContext.Configuration.LazyLoadingEnabled = false;
I think option number 2 would be preferred as that leaves you the choice when to enable/disable lazy loading.