Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude all in Entity Framework (lazy loading enabled)

In my ASP.NET MVC application I have lazy loading for Entity Framework turned on.

It means that it loads all the data from all projects relations when I use eg.

@using (MyApp.Models.ProjectsEntities db = new VodenjeDel.Models.ProjektiEntities()) {
    db.projects;
}

When in some cases I don't want to load all related data, I simply add .Include(x => x.Colors), which means that all related data except colors are excluded, since if there is no include used, it loads all related data.

But in some very rare cases, I want to exclude all related data and get data only from projects.

I want to use something like:

db.projects.Exclude(x => x.All);

But this doesn't work. What is the correct way to do this with lazing loading enabled?

like image 358
Tadej Avatar asked Feb 09 '23 19:02

Tadej


2 Answers

you can disable lazy loading whenever you want to..

@using (MyApp.Models.ProjectsEntities db = new VodenjeDel.Models.ProjektiEntities()) {
    db.Configuration.LazyLoadingEnabled = false;
    var projects = db.projects; // no related data available

    db.Configuration.LazyLoadingEnabled = true;
    var projects = db.projects; // related data available

}
like image 122
JamieD77 Avatar answered Feb 12 '23 12:02

JamieD77


I think you have how lazy loading works confused. If you have lazy loading enabled, then any object you have that you didn't load the property will go and load it when you try to use that property.

Using .Include is called Eager loading, and will grab the requested children at the same time it is grabbing the base data.

If you don't want any properties, then just call the base object and don't try to access any of the child properties.


In my MVC application I have Lazy loading for entity framework turned on.

It means that it loads all the data from all projects relations when I use eg.

That is incorrect. That will load all the base properties of project.

When/IF you try to go and access a child property, it will then at that point go and retrieve the related data.


With lazy loading DISABLED:

@using (MyApp.Models.ProjectsEntities db = new VodenjeDel.Models.ProjektiEntities()) {
    var results=db.projects.ToList();
    // results[0].Children is empty at this point
    var child=results[0].Children; // child is empty
}

With lazy loading ENABLED:

@using (MyApp.Models.ProjectsEntities db = new VodenjeDel.Models.ProjektiEntities()) {
    var results=db.projects.ToList();
    // results[0].Children is empty at this point
    var child=results[0].Children; // This causes a DB lookup to get the children

}

With eager loading:

@using (MyApp.Models.ProjectsEntities db = new VodenjeDel.Models.ProjektiEntities()) {
    var results=db.projects.Include(x=>Children).ToList();
    // results[0].Children is already in memory
    var child=results[0].Children; // This doesn't cause a DB lookup, and the data is returned from memory

}
like image 37
Robert McKee Avatar answered Feb 12 '23 10:02

Robert McKee