locked
Disable Lazy Loading for Navigation Properties on Entities in EF 5 RRS feed

  • Question

  • Hello all,

    Can we disable Lazy Loading for Navigation properties on any entity?

    If yes, how we can achieve the same.

    Thanks,
    Naresh

    Friday, September 7, 2012 10:56 PM

Answers

  • Hi Naresh;

    If you are using code first you can do the following to turn off Lazy loading for a given navigation property. For example lets say we have a Customers table and a Orders table and a Customer can have 1 to many Orders the following POCO class has a ICollection that is mark as virtual, This causes Lazy loading to happen when a property of order is accessed.

    // Lazy Loading Enabled
    public class Customers { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Order> Orders { get; set; } }

    To turn off Lazy loading remove the virtual key word as follows:

    // Lazy Loading Is Disabled 
    public class Customers
    { 
        public int Id { get; set; } 
        public string Name { get; set; } 
        public ICollection<Order> Orders { get; set; } 
    }

    To turn of Lazy Loading for all entities using Code First you can do the following in the constructor of the DbContext.

    public class MyContext : DbContext
    {
        public MyContext()
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
    }

    If the above does not meet your needs please tell us what version of the Entity Framework, which context type you are using, for example ObjectContext or DbContext, and which modeling you are using such as Code First, Model First or Database First.

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Alexander Sun Wednesday, September 19, 2012 7:06 AM
    Saturday, September 8, 2012 3:11 AM

All replies

  • Hi Naresh;

    If you are using code first you can do the following to turn off Lazy loading for a given navigation property. For example lets say we have a Customers table and a Orders table and a Customer can have 1 to many Orders the following POCO class has a ICollection that is mark as virtual, This causes Lazy loading to happen when a property of order is accessed.

    // Lazy Loading Enabled
    public class Customers { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Order> Orders { get; set; } }

    To turn off Lazy loading remove the virtual key word as follows:

    // Lazy Loading Is Disabled 
    public class Customers
    { 
        public int Id { get; set; } 
        public string Name { get; set; } 
        public ICollection<Order> Orders { get; set; } 
    }

    To turn of Lazy Loading for all entities using Code First you can do the following in the constructor of the DbContext.

    public class MyContext : DbContext
    {
        public MyContext()
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
    }

    If the above does not meet your needs please tell us what version of the Entity Framework, which context type you are using, for example ObjectContext or DbContext, and which modeling you are using such as Code First, Model First or Database First.

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Alexander Sun Wednesday, September 19, 2012 7:06 AM
    Saturday, September 8, 2012 3:11 AM
  • Thanks for the reply Fernando.

    I am using Databsse first approach.

    I tried to disable lazy loading using edmx properties. But I doubt it's not working for me.

    Can you please help me on the same.

    Thanks,
    Naresh

    Saturday, September 8, 2012 9:02 PM
  • Hi Naresh;

    OK as you did in the designer by turning off LazyLoadingEnabled to false should have turned off lazy loading. So please show the code you are using and tell me how you determined that it is still enabled.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Sunday, September 9, 2012 3:02 PM
  • for some reason when I do this:

        this.Configuration.LazyLoadingEnabled = false;

    where I had virtual properties and I got values, I am now getting null for everything and hence my app breaks. Any ideas?


    • Edited by CodingD Friday, March 8, 2013 8:21 PM
    Friday, March 8, 2013 8:21 PM
  • When I was wanting to do this it turned out I really just wanted to use the Include statement when I pulled an item from the collection.

    So go from this:

    result = db.Companies.Where(w => w.CompanyUrl == "aaa").FirstOrDefault();

    To this:

    result = db.Companies.Include("Locations").Where(w => w.CompanyUrl == "aaa").FirstOrDefault();

    That will disable the lazy load for that usage so that I can still use the Company.Locations property after the EF Context has been disposed.

    Tuesday, January 6, 2015 10:21 PM