EF: Querying a collection in an attached entity where the detached entity did not populate the collection
<p>Trying to find this answer online has been difficult, so I'm hoping I'm not asking a stupid question here.</p>

<p>I have two classes, <code>User</code> and <code>Login</code>.  A <code>User</code> can have zero or more <code>Login</code>s.  A <code>Login</code> has exactly one <code>User</code>.  (ASP.NET Identity)</p>

<p>Building out my <code>IUserLoginStore</code> implementation, I wrote these methods:</p>

<pre><code>public Task&lt;IList&lt;UserLoginInfo&gt;&gt; GetLoginsAsync(User user)
{
    return Task.Run(() =&gt; GetLogins(user));
}

private IList&lt;UserLoginInfo&gt; GetLogins(User user)
{
    using (var context = _contextFactory.Get())
    {
        user = context.Users.Attach(user);
        var logins = new List&lt;UserLoginInfo&gt;();
        logins.AddRange(user.Logins.Select(x =&gt; new UserLoginInfo(x.LoginProvider, x.ProviderKey)));
        return logins;
    }
}
</code></pre>

<p>... now, in unit testing, I was mocking the <code>context.Users.Attach(User user)</code> method to return a <code>User</code> object with a populated <code>IList&lt;Login&gt;</code>, but... I'm not sure if EF would behave that way.</p>

<p>If I attach an entity to an EF database context as above, but the previously detached entity did not have the Logins property populated due to lazy loading, will I be able to access the Logins property after attaching it, or will it be null, throwing a <code>NullReferenceException</code> when executing this method?</p>

<p>I'm not ready to start integration testing yet, so I can't verify what the behavior will be.  I don't know if my unit tests are valid, as a result.  Can someone educate me on this?</p>