none
Why navigation properties are loaded during class construction, but then never again RRS feed

  • Question

  • I have a Singleton class which is where I manage all tables, query the database, etc.

    public class clsTableManager
    {
        private static clsTableManager instance;
    
        public List<Entities> entities { get; set; }
    
        #region Constructors
        /// <summary>
        /// Default constructor. Initialises all Lists. Acts as a cache.
        /// </summary>
        private clsTableManager()
        {
            using (FCERTSModelContainer db = new FCERTSModelContainer())
            {
                entities = db.Entities.ToList<Entities>();
            }
        }
    
        public static clsTableManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new clsTableManager();
                }
                return instance;
            }
        }
    }

    This is fine, works well. Entities has a Navigation Property to another table, it gets built fine and I can reference the varies entries in the second table.
    My problem comes later on. If I try and refresh the "entities" list, by calling a method which looks exactly like the constructor, it gets all the latest Entities, but not all of the references are built. It sometimes builds some, more often than not it's never the ones I actually need. I've found that using db.Entities.Include()... will force the references to be built. I am struggling to understand why they're built in the constructor though and not in the refresh method?

    I create the singleton at the very begging of my program (during form initialisation) but the navigation properties aren't used until a use clicks a button, so it doesn't appear that I'm accessing them without realising it. If someone could explain a bit more clearly for me, it would be much appreciated.

    Tuesday, January 29, 2013 6:52 AM

Answers

  • Hi FizzBuzz791,

    I think it is caused by the strategy of loading related entities. I guess you are using lazy loading. For lazy loading, the navigation property will be loaded when it is accessed first time. You could check this in SQL profile if you have interest. If you use Include method, you were performing a eagerly loading, another loading strategy. Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method. Thus, you are able to get all the navigation property. You could refer to this page for more information: http://msdn.microsoft.com/en-us/data/jj574232.aspx

    Good 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.


    Tuesday, February 5, 2013 6:51 AM

All replies

  • Hi FizzBuzz791,

    Welcome to the MSDN forum.

    You said it gets all the latest Entities, but not all of the references are built. Could you please let me know which reference is built and which reference is not built?

    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.

    Thursday, January 31, 2013 2:20 AM
  • Hi Alexander,

    It seems to be at random. Say I have 6 rows, each with a reference to a second table (doesn't have to be a unique reference, they could be referencing the same row in the second table) about 50% will be fully built, i.e. I can navigate through them, while the others are null. I honestly couldn't see a pattern to it. Using include() works fine, so I'm not overly concerned. I was just wondering if someone might have an idea why they're being built the first time I call Entities.ToList() but not on subsequent calls.

    Monday, February 4, 2013 12:37 AM
  • Hi FizzBuzz791,

    I think it is caused by the strategy of loading related entities. I guess you are using lazy loading. For lazy loading, the navigation property will be loaded when it is accessed first time. You could check this in SQL profile if you have interest. If you use Include method, you were performing a eagerly loading, another loading strategy. Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method. Thus, you are able to get all the navigation property. You could refer to this page for more information: http://msdn.microsoft.com/en-us/data/jj574232.aspx

    Good 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.


    Tuesday, February 5, 2013 6:51 AM