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

  • Home
  • SEARCH
  • 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 5948997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:11:15+00:00 2026-05-22T17:11:15+00:00

I’m new to Entity Framework and uncertain as to when to use eager loading

  • 0

I’m new to Entity Framework and uncertain as to when to use eager loading and Include, when to use LINQ queries with Select, and when to load entities on demand. I also don’t know if and when EF caches entities. (Yes, I’ll read the docs eventually 🙂

I’m building a messaging system internal to ASP .NET MVC website, and performance is going to be really important. Imagine the following scenario.

When Rihanna goes into her inbox, she needs to see a list of messages, each with sender’s name and picture. Imagine John wrote Rihanna lyrics to his new song. In her inbox, there is a message with John’s picture. When John goes into his outbox, he sees the same message with Rihanna’s name and picture.

So, when we grab a message from the database, we might need both sender and recipient profile pictures. Imagine the following query:

private static Func<MyEntities, int, Message> _getMessageByIdQuery = CompiledQuery.Compile(
    ( MyEntities ctx, int messageId ) => ctx.Messages
        .Include( "SenderUser" )
        .Include( "SenderUser.UserProfile.Image" )
        .Include( "RecipientUser" )
        .Include( "RecipientUser.UserProfile.Image" )
        .SingleOrDefault( message => message.ID == messageId ) );

Will this query be efficient? Is there any way to load SenderUser.UserProfile.Image without loading SenderUser, considering User.ID and UserProfile.UserID are one-to-one? I imagine I would need to use LINQ JOIN’s for this. Is this correct?

Is it more efficient to use LINQ to avoid loading all fields when only one is needed (e.g. only load ImageID from UserProfile)?

Is it better to do the same using Load somewhere in code?

Does such query go to the database each time? Imagine we had a thousand messages, all from the same person. Would we query for his Image each time?

I’m using EF 4 and ASP .NET MVC 3.

Thanks.

  • 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-22T17:11:16+00:00Added an answer on May 22, 2026 at 5:11 pm

    One way would be to send custom display/data objects directly from the query, that way you do not need to load extra objects. Example:

    var dto = ctx.Messages
              .Where(whereclause)
              .Select(m=> new DTOTYPE
                             {Id = m.Id, 
                              Content= m.Content, 
                              SenderId = m.SenderUser.Id, 
                              SenderImage = m.SenderUser.UserProfile.Image, 
                              SenderImageId = m.SenderUser.UserProfile.ImageId}) 
                             //more fields if you need any
    

    This will depend on your scenario though, but this way you do not have to do crazy includes all over. But you’ll need to manage your view/data objects as they might get out of hand for many different screens and views. Hope this helps.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a jquery bug and I've been looking for hours now, I can't

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.