NullReferenceException in EntityFramework, how come?
        Posted  
        
            by Mickel
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Mickel
        
        
        
        Published on 2010-04-21T12:15:48Z
        Indexed on 
            2010/04/21
            12:33 UTC
        
        
        Read the original article
        Hit count: 360
        
Take a look at this query:
var user = GetUser(userId);
var sessionInvites = ctx.SessionInvites
    .Include("InvitingUser")
    .Include("InvitedUser")
    .Where(e => e.InvitedUser.UserId == user.UserId)
    .ToList();
var invites = sessionInvites;
// Commenting out the two lines below, and it works as expected.
foreach (var invite in sessionInvites)
    ctx.DeleteObject(invite);
ctx.SaveChanges();
return invites;
Now, everything here executes without any errors. The invites that exists for the user are being deleted and the invites are being returned with success.
However, when I then try to navigate to either InvitingUser or InvitedUser on any of the returned invites, I get NullReferenceException. All other properties of the SessionIvites returned, works fine.
How come?
[EDIT] Now the weird thing is, if I comment out the lines with delete it works as expected. (Except that the entities will not get deleted :S)
© Stack Overflow or respective owner