How to Turn off lazy loading for all entities in Entity Framework

By FoxLearn 2/16/2024 3:11:01 PM   88
This post shows you how to Turn off lazy loading for all entities in Entity Framework
public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
    {
        this.Configuration.LazyLoadingEnabled = false;
    }
}

If you want to do with an explicit call.

var post = context.Posts.Find(1);

// Load the blog related to a given post.
context.Entry(post).Reference(p => p.Blog).Load();

var blog = context.Blogs.Find(1);

// Load the posts related to a given blog.
context.Entry(blog).Collection(p => p.Posts).Load();