none
How to custom Load a navigation property in Entity Framework preserving lazy loading RRS feed

  • Question

  • Hi,

    I have a problem with loading of navigation properties (Lazy loading is enabled). I need to custom load navigation property when I access it.

    Requirement for doing this raised due to making sorted list (order by any specific column of navigation entity).

    I have below models

     public class Blog
        {
            public int BlogId { get; set; }
            public string Name { get; set; }

            public virtual List<Post> Posts { get; set; }
        }

        public class Post
        {
            public int PostId { get; set; }
            public string Title { get; set; }
            public string Content { get; set; }

            public int BlogId { get; set; }
            public virtual Blog Blog { get; set; }
       

    }

      static void Main(string[] args)
            {
                using (var db = new BloggingContext())
                {

    Blog blog = db.Set<Blog>().FirstOrDefault();

               }

                }

    I need a common method event where I can get navigation property(specifically list type) for any entity and make that order by any specific column of navigation entity. In above code I would like to sort blog .Post .

    After doing much googling I found there is an event in ObjectContext named "ObjectMaterialized" where I can do the same, but problem with this is that I loose lazy loading feature of Entity frame work.

    Below is event where I sorted  blog .Post list.

    void ObjectContext_ObjectMaterialized(object sender, System.Data.Objects.ObjectMaterializedEventArgs e)
            {
                if (e.Entity is Blog)
                {
                    Blog blog = (Blog)e.Entity;
                    blog.Posts.OrderByDescending(t=>t.Title);
                }
              }

    Here Post list navigation property of blog will already be populated prior to requesting in my business logic.

    That means If I need blog.Post list somewhere in my method in business logic it will already be populated from database.

    So this is actual problem I have, I want that I can sort my list type navigation property without loosing lazy loading feature(Although lazy loading is being applied as it is in above code but I mean to say it that I want it when I access navigation property in business logic).

    Thanks,

    Sanjeet


    • Moved by CoolDadTx Tuesday, May 14, 2013 1:52 PM EF related
    Tuesday, May 14, 2013 1:29 PM

Answers

  • Hi Sanjeet,

    I didn't find there are any properties to enable automatic sorting for the list.

    I'm not sure if it is possible in your project, I think you may have a try to create custom properties which return the sorted list in partial classes. Use the custom properties rather than navigation properties if sorted list is needed.

    Best regards,


    Chester Hong
    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, May 21, 2013 8:15 AM
    Moderator

All replies

  • Hi,

    Still a bit unclear to me. It seems to me that the issue is that you try to sort the data before you really need them. What if you just try to add this sort order where you already actually use those data ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Tuesday, May 14, 2013 1:52 PM
  • Thanks Patrice for reply, You got the right, I really need to sort the data before you really need them.

    I can do that, but the application I am working on need every navigation property of List type to be sorted by a specific column. If I choose to sort those list when needed then I will have to make changes in lot of code files(around more than 200).  So I am looking for an alternative for this.

    I am just thinking if Entity Framework provide any method,properties or event to override related with entity loading so that I can use that.

    Tuesday, May 14, 2013 5:28 PM
  • Hi Sanjeet,

    I didn't find there are any properties to enable automatic sorting for the list.

    I'm not sure if it is possible in your project, I think you may have a try to create custom properties which return the sorted list in partial classes. Use the custom properties rather than navigation properties if sorted list is needed.

    Best regards,


    Chester Hong
    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, May 21, 2013 8:15 AM
    Moderator