title: .NET Core Identity using userManager and load additional ApplicationUser properties
body: <p>My question is based on the topic <a href="https://stackoverflow.com/questions/48623533/how-to-load-navigation-properties-on-an-identityuser-with-usermanager">here</a> but as I am not allowed to add comments there I guess I have to open a new thread.</p>
  
  <p>In my ASP.NET Core backend I'm using ASP.NET Core Identity for the user management. It provides methods for user authentication with the help of the so-called user manager.</p>
  
  <p>I now have troubles with using methods from the user manager on the one hand and loading custom Application User properties on the other hand.</p>
  
  <p>Scenario as follows: the frontend sends via MVC controller (HTTP request) a login dto that contains an email address and a password. I now want to load the application user with the method <code>FindByEmailAsync</code> provided by the user manager. I wrap this method in a self-made application user repository where the user manager is injected by dependency injection and that has also access to the application context. As the user manager does not load custom properties when loading the user manager I have to do some additional stuff to load these properties, too.</p>
  
  <p>I first tried to load the user completely via the context (not via user manager). That worked fine and the additional properties could be loaded, too. The problems started when afterwards trying to use the <code>usermanager.CheckPasswordAsync</code> method. I got an exception:</p>
  
  <blockquote>
    <p>"the instance of entity ApplicationUser cannot be tracked
    because another instance with the same key value for id is already
    been tracked".</p>
  </blockquote>
  
  <p>I think this resulted from using the context to load the user on the one hand and the user manager to perform the <code>checkPassword</code>. Then I tried to load the user with the <code>FindByEmailAsync</code> of the <code>usermanager</code>. Everything worked fine, no exceptions, but the custom properties have not been loaded. In the thread that I linked above there are some suggestions but none of them works for me.</p>
  
  <ol>
  <li><p>Suggestion:</p>
  
  <pre><code>var user = await userManager.Users
    .Include(x =&gt; x.Address)
    .SingleAsync(x =&gt; x.NormalizedEmail == email);
  </code></pre></li>
  </ol>
  
  <p><code>userManager.Users</code> is a <code>IQueryable&lt;Users&gt;</code> and does not contain an Include method.</p>
  
  <ol start="2">
  <li><p>Suggestion:</p>
  
  <pre><code>await context.Entry(user).Reference(x =&gt; x.Address).LoadAsync();
  </code></pre></li>
  </ol>
  
  <p>In my case the custom property (Certificates) is a collection so I used <code>.Collection</code> instead of <code>.Reference</code>, but got the following error:</p>
  
  <blockquote>
    <p>"Navigation property 'Certificates' on entity of type
    'ApplicationUser' cannot be loaded because the entity is not being
    tracked. Navigation properties can only be loaded for tracked
    entities."</p>
  </blockquote>
  
  <p>Does anybody has a suggestion how to use the provided methods of the user manager and including custom properties to the application user?</p>
  
  <p>Thank you!</p>