Entity EF6 opposite of include

c# ef-code-first entity-framework entity-framework-6

Question

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!

1
3
3/1/2016 11:38:01 AM

Popular Answer

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

  1. Remove the virtual keyword all together from the properties you never want lazily loaded.

  2. 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.

2
3/1/2016 11:49:19 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow