none
Entity Framework 4 - Disabling Lazy Loading Does not work for my code RRS feed

  • Question

  • I am using EF 4 Code First with DbContext based POCO objects. I have disabled Lazy Loading in my DbContext which works fine in some cases and sometimes I find EF still populates navigation properties of few entities. This specially happens on second or third call to same BO method.

    Here is how I know Lazy Load is disabled and work sometimes :

    public class Goods : IGoods
           {
    public Int64 ID { get; set; }
            public Int64 UniqueGoodsId { get; set; }        
            public String Caption { get; set; }

    [ForeignKey("User")]
            public Int64 UserId { get; set; }
            public User User { get; set; }

    ForeignKey("Store")]
    public Int64 StoreId { get; set; }        
            public Store Store { get; set; }
    }

    public class Remark
           {
    public Int64 ID { get; set; }

    [ForeignKey("Goods")]
            public Int64 GoodsId { get; set; }
            public Goods Goods { get; set; }

            public String RemarkDesc { get; set; }
            public bool IsApproved { get; set; }
    }

    Once lazy Load is disabled and when I access Remark entity without using "Include" for "Goods", it work fine for first few calls and I can see that "Remark" has navigation property Goods set to Null.
    But as I keep debugging for another 5-6 calls, Goods object is populated and its all navigation properties are populated too. This causes serialization logic to get in infinite loop or throws StackOverflowException

    Any help in this is highly appreciated. Thanks

    Here are the things I have already tried to correct this behavior:

    1. Disabled Lazy Load:

    this.Configuration.LazyLoadingEnabled = false;

    2. Disabled Proxy Creation:

    this.Configuration.ProxyCreationEnabled = false;

    3. Removed "virtual" keyword from all navigation properties.

    4. also, I am using Dependency Injection with Generic Repository pattern.

    Any help in this is highly appreciated. Thanks

    -Savita


    • Edited by Geeker S Friday, December 14, 2012 12:28 AM
    Friday, December 14, 2012 12:23 AM

All replies

  • Hi Savita,

    Welcome to the MSDN forum.

    Did the navigation property (Goods) of remark object is populated after another 5-6 calls?

    Could you please show us the related snippet code of “another 5-6 calls”?

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 17, 2012 5:10 AM