0

By default I want my Linq to SQL calls to include the child entities so I have LazyLoadingEnabled set to true. On a small subset of calls I want to return only the parent entities without the related child entities included. I tried setting LazyLoadingEnabled to false just before my linq query, and then setting it back to true just after but that had no effect.

Is there a way to achieve this?

Example Model:

public class UserProjectInternalMember
{
    [Key, Column(Order = 0), ForeignKey("UserProject")]
    public int UserProjectId { get; set; }

    [Key, Column(Order = 1), ForeignKey("User")]
    public string UserId { get; set; }

    [Column(Order = 2), ForeignKey("UserProjectMembershipLevel")]
    public int UserProjectMembershipLevelId { get; set; }

    public virtual UserProject UserProject { get; set; }
    public virtual ApplicationUser User { get; set; }
    public virtual UserProjectMembershipLevel UserProjectMembershipLevel { get; set; }

    [NotMapped]
    public bool CanEdit { get; set; }

    [NotMapped]
    public bool CanRemove { get; set; }
}
Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98
Mark Seymour
  • 27
  • 13
  • 1
    Please can you paste your entity class here. It may be due to your Navigation Property dont have Virtual that could result in eager loading. – mongesh madhavan Jul 25 '18 at 14:45
  • I've added an example of my models above. All navigational properties are virtual at each end. – Mark Seymour Jul 26 '18 at 08:47
  • See this question https://stackoverflow.com/questions/24022957/entity-framework-how-to-disable-lazy-loading-for-specific-query Check second asnwer you can do it for paritcular query like that. – mongesh madhavan Jul 26 '18 at 09:19
  • As mentioned in the question I already tried the suggestion in that thread and it had no effect. – Mark Seymour Jul 26 '18 at 09:57

2 Answers2

1

First, let's clear up some misconceptions lurking in the first sentence:

By default I want my Linq to SQL calls to include the child entities so I have LazyLoadingEnabled set to true

  1. Lazy loading is the default. You have to set it to false if you want to disable it.
  2. Lazy loading doesn't include child entities. It loads them later when the navigation properties are accessed and the context is still alive. (And has lazy loading enabled).
  3. Entity Framework != LINQ-to-SQL.

Now about your question.

Entity object are created by a context either with or without lazy loading capabilities. This decision is controlled by the setting context.Configuration.ProxyCreationEnabled, not LazyLoadingEnabled. After materialization, an entity object's fate is sealed: it is or isn't capable of lazy loading.

When a lazy-loading navigation property is accessed in an object that is capable of lazy loading, lazy loading will occur if the context is still alive and has LazyLoadingEnabled = true at that moment (not at the moment of creating the object).

That's why flipping the value of LazyLoadingEnabled didn't have the effect you intended. If you want to create objects that won't ever lazy-load you have to create them while ProxyCreationEnabled is false.

Gert Arnold
  • 93,904
  • 24
  • 179
  • 256
0

You can change the Lazy Loading on the fly in the context

yourDBContext.Configuration.LazyLoadingEnabled = false;

Alternatively, if you want to just disable it for a particular navigation property, then just make the property non-virtual.

Or, another way which I prefer is to disable Lazy Loading entirely and use eager loading as and when you really need to.

Mark Bennetts
  • 133
  • 10
  • I tried your on-the-fly suggestion and it had no effect for the linq call I was testing with. Unfortunately it's not on a property by property basis, but on a data-call by data-call process so your second suggestion is not suitable. And finally, turning off Lazy Loading entirely and converting calls that need eager loading is not an option as it is too late into the project lifespan. There would be too much refactoring required to make it viable. Thank you for your suggestions, though. – Mark Seymour Jul 26 '18 at 09:54
  • Are you completely sure - I'm convinced it should work, e.g. `using(var context = new YourDBContext()) { context.Configuration.LazyLoadingEnabled = false; // do your thing here, but make sure you're careful to manually include any // relationships that you do need now }` – Mark Bennetts Jul 26 '18 at 10:27