Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, the different behaviour is normal and it occurs because of <em>Relationship Fixup</em>.</p> <p>When you load only one <code>SiteMember</code> with <code>FirstOrDefault</code> the two collections <code>InvitationsSent</code> and <code>InvitationsReceived</code> will be loaded as well as soon as you access them (or inspect them in the debugger) because they are marked as <code>virtual</code> and lazy loading will apply. But the <code>FriendshipInvitation</code>s in those collections won't lazily load the <code>InvitingUser</code> or the <code>InvitedUser</code> because these navigation properties are <em>not</em> marked as <code>virtual</code>.</p> <p>Say, <code>SiteMember</code> 1 that you load with <code>FirstOrDefault</code> has an invitation received from <code>SiteMember</code> 2 and no invitation sent. So, <code>InvitationsReceived</code> will contain one element and <code>InvitationsSent</code> will be empty. That element - a <code>FriendshipInvitation</code> entity - will have <code>InvitingUser</code> as 2 and <code>InvitedUser</code> as 1. The <code>InvitingUser</code> will be <code>null</code> in that case because it can't be lazily loaded (since it isn't <code>virtual</code>) and the <code>SiteMember</code> 2 isn't loaded yet into the context. (<code>InvitedUser</code> however will be a dynamic proxy to <code>SiteMember</code> 1 because you already have loaded the <code>SiteMember</code> 1, see next paragraph.)</p> <p>However, if you load <em>all</em> <code>SiteMember</code>s with <code>ToList()</code> the <code>SiteMember</code> 2 will be loaded as well into the context with the query executed by <code>ToList()</code>. EF will set the <code>InvitingUser</code> 2 automatically to the already loaded entity which is called <em>Relationship Fixup</em>. It is <em>not</em> loaded by lazy loading of the <code>InvitingUser</code> navigation property because lazy loading isn't supported for a not <code>virtual</code> property.</p> <p>To have a consistent behaviour and not rely on Relationship Fixup alone I would suggest that you mark the <code>InvitingUser</code> and <code>InvitedUser</code> navigation properties as <code>virtual</code> as well.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload