Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this normal Entity Framework behaviour
    primarykey
    data
    text
    <p>I have two one to many relations between the following classes</p> <pre><code>public class SiteMember { public int SiteMemberID { get; set; } public virtual List&lt;FriendshipInvitation&gt; InvitationsSent { get; set; } public virtual List&lt;FriendshipInvitation&gt; InvitationsReceived { get; set; } } public class FriendshipInvitation { public int InvitingUserID { get; set; } public SiteMember InvitingUser { get; set; } public int InvitedUserID { get; set; } public SiteMember InvitedUser{ get; set; } } </code></pre> <p>Then in controller I have the following code (which loads one SiteMember from db):</p> <pre><code>SiteMember sm = repoSiteMember.GetUserByName(User.Identity.Name); </code></pre> <p>at this point </p> <pre><code>sm.InvitationsReceived.InvitingUserId = 1; sm.InvitationsReceived.InvitingUser = null </code></pre> <p>Then I have the following line (which loads all SiteMembers from db):</p> <pre><code>IEnumerable&lt;SiteMember&gt; all = repoSiteMember.GetAll(); </code></pre> <p>after which </p> <pre><code>sm.InvitationsReceived.InvitingUserId = 1 sm.InvitationsReceived.InvitingUser = proxy to SiteMember object. </code></pre> <p>My question is: Is this normal behaviour? I'm quite sure I can "fix" null pointer with include statement, but I find it weird that loading all elements from db actually changes the state of object. (null => proxy). Should the proxy be there from the beginning?</p> <p><strong>Edit:</strong> Just to confirm, include statement does solve the problem of null reference, but that was not the question here.</p> <pre><code>DataContext.Members .Include("InvitationsSent.InvitedUser") .Include("InvitationsReceived.InvitingUser") .FirstOrDefault(e =&gt; e.UserName == userName); </code></pre> <p><strong>Edit2:</strong> Methods included now.</p> <pre><code>public SiteMember GetUserByName(string userName) { return base.DataContext.Members.FirstOrDefault(e =&gt; e.UserName == userName); } public IEnumerable&lt;SiteMember&gt; GetAll() { return base.DataContext.Members.ToList(); } </code></pre> <p><strong>Edit3:</strong> You may need that to recreate the model. </p> <pre><code> modelBuilder.Entity&lt;FriendshipInvitation&gt;() .HasRequired(e =&gt; e.InvitedUser) .WithMany(e =&gt; e.InvitationsReceived) .HasForeignKey(e =&gt; e.InvitedUserID) .WillCascadeOnDelete(false); modelBuilder.Entity&lt;FriendshipInvitation&gt;() .HasRequired(e =&gt; e.InvitingUser) .WithMany(e =&gt; e.InvitationsSent) .HasForeignKey(e =&gt; e.InvitingUserID); modelBuilder.Entity&lt;FriendshipInvitation&gt;() .HasKey(e =&gt; new { e.InvitingUserID, e.InvitedUserID, e.InvitationNo }); </code></pre>
    singulars
    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.
 

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