title: How to track identity changes in EF Core?
body: <p>I have a service in my application which creates a User, and saves it to the database. Here's a method of my service:</p>
  
  <pre><code>    public async Task&lt;UserDTO&gt; CreateUserAsync(User newUser)
      {
  
          var result = (UserDTO)await _userRepository.CreateUserAsync(newUser);
  
          if (result != null)
          {
  
              await _userRepository.SaveChangesAsync();
  
          }
  
          return result;
  
      }
  </code></pre>
  
  <p>And a method from a UserRepository:</p>
  
  <pre><code>public async Task&lt;User&gt; CreateUserAsync(User newUser) =&gt; (await _dbContext.AddAsync(newUser)).Entity;
  </code></pre>
  
  <p>Here's a User class:</p>
  
  <pre><code>public class User
  {
  
      public int UserId { get; set; }
      public string Username { get; set; }
      public string Password { get; set; }
      public string Name { get; set; }
      public string Surname { get; set; }
  
  }
  </code></pre>
  
  <p>The problem is when the user is being added via service, UserId is not known yet. It has default value 0, then ef core saves it to a database, finding a proper UserId. But value returned by my methods has no UserId updated - it is still 0, and i would like to return updated value. How to achieve that in a proper way? </p>