title: DbContext - how do I stop my database from creating new records when I'm updating them?
body: <p>My database keeps creating new records when it should be updating the current one. Razor-Pages project, asp.net 2.2.</p>
  
  <p>What should be happening:</p>
  
  <ol>
  <li>Retrieving email from asp.net Identity user class</li>
  <li>Finding match in table Companies by comparing email == email</li>
  <li>Updating records</li>
  <li>Saving changes</li>
  </ol>
  
  <p>And instead of updating, it creates new records.</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; OnGetAsync()
  {
      var user = await _userManager.GetUserAsync(User);
      if (user == null)
      {
          return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
      }
  
      var email = await _userManager.GetEmailAsync(user);
  
      var company = _context.Companies.FirstOrDefault(b =&gt; b.Email == email);
      Firma = user.Firma;
  
      return Page();
  }
  
  public async Task&lt;IActionResult&gt; OnPostAsync()
  {
      var user = await _userManager.GetUserAsync(User);
      if (user == null)
      {
          return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
      }
  
      var email = await _userManager.GetEmailAsync(user);
  
      var company = _context.Companies.First(a =&gt; a.Email == email);
  
      company.Navn = Input.Firma;
      company.Postnr = Input.Postnr;
      company.Sted = Input.Sted;
      company.Beskrivelse = Input.Beskrivelse;
  
      _context.SaveChanges();
  
      return RedirectToPage();
  }
  </code></pre>