Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3280942
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:45:39+00:00 2026-05-17T19:45:39+00:00

I commented out the line of code that enables lazy loading from all the

  • 0

I commented out the line of code that enables lazy loading from all the ctor overloads for my ObjectContext class, yet when I iterate over a navigational property, the iteration is successful. Why?

Here’s the relevant bits of code.

    public MyExpensesEntities() : 
                 base("name=MyExpensesEntities", "MyExpensesEntities")
    {
        // this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    static void Main(string[] args)
    {
    AddExpenses();

       Console.WriteLine("Lazy loading is {0}.", 
                         _context.ContextOptions.LazyLoadingEnabled ? 
                         "enabled": "disabled");

       PrintCategorywiseExpenses();

   _context.Dispose();
       Console.WriteLine("Press any key to exit...");
   Console.ReadKey();
    }

 static void PrintCategorywiseExpenses()
    {
        foreach (var cateogry in _context.Categories)
        {
            Console.WriteLine
                            ("Category: {0}\n----------------", 
                             cateogry.CategoryName);

            foreach (var e in cateogry.Expenses)
                Console.WriteLine
                                    ("\tExpense: {0}\tAmount: {1}", 
                                     e.Particulars, e.Amount.ToString("C"));
        }

        Console.WriteLine();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:45:40+00:00Added an answer on May 17, 2026 at 7:45 pm

    You should be aware that by doing a foreach over a navigation property you are explicitly ask EF to load it for you and just because you turn off the Lazy Loading does not mean that you cannot explicitly load them later. So, once it is disabled, you can still explicitly load related data on demand if needed and that’s exactly what happened when you use foreach. Another term for “Lazy Loading” is actually “implicit deferred loading” and it will not prevent us from doing an “Explicit deferred loading”

    Basically, this line of code:

    foreach (var cateogry in _context.Categories)

    is almost equivalent to this:

    _context.Categories.AsEnumerable()
    

    To see how disable lazy loading affect your code see the example below.

    In this example, I disable it and read one of your Categories then dispose the ObjectContext and do the foreach over Expenses:

    Category category;
    using (MyEntities context = new MyEntities()) {
        category = _context.Categories.First();
    }
    
    foreach (var e in cateogry.Expenses)
          Console.WriteLine("Expense:{0}", e.Amount.ToString("C"));
    }
    

    LazyLoading is Disabled:

    Nothing happens, the code will never get into the foreach since the cateogry.Expenses.Count == 0

    LazyLoading is Enabled:

    EF tries to lazy load Expenses and since the ObjectContext has been disposed (by going out of using scope) you’ll get an System.ObjectDisposedException containing this message:

    The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my code. See the line that is commented out. When the element id
It just happened to me that I commented out a line of code for
When a piece of code is commented we say just that, it's commented out.
I have a block of code below with a single line commented out. What
The code below works. But if I comment out the line Dim objRequest As
The django csrf middleware can't be disabled. I've commented it out from my Middleware
I have a class 'Downloader' derived from QObject that runs in a worker thread.
My application is suspending on a line of code that appears to have nothing
I have a code file from the boto framework pasted below, all of the
Is there a way to comment out a single line in HTML using just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.