title: The instance of entity type 'BookLoan' cannot be tracked
body: <p>I'm trying to update an entity and I've run into the following error: </p>
  
  <blockquote>
    <p>InvalidOperationException: The instance of entity type 'BookLoan'
    cannot be tracked because another instance of this type with the same
    key is already being tracked. When adding new entities, for most key
    types a unique temporary key value will be created if no key is set
    (i.e. if the key property is assigned the default value for its type).
    If you are explicitly setting key values for new entities, ensure they
    do not collide with existing entities or temporary values generated
    for other new entities. When attaching existing entities, ensure that
    only one entity instance with a given key value is attached to the
    context.</p>
  </blockquote>
  
  <p>I've done a little research and from what I can tell I'm apparently trying to track an already tracked entity when I use <code>_context.Update(bookloan);</code> but I'm not really sure what to do.</p>
  
  <p>What I'm trying to do is update an existing entity/record in my database. Here are the get and post controllers as I'm not sure what else to share.</p>
  
  <p>Get</p>
  
  <pre><code>    [HttpGet]
      public async Task&lt;IActionResult&gt; Return(int? id)
      {
          if (id == null)
          {
              return NotFound();
          }
  
          if (isBookCheckedOut(id) == false)
          {
              //Not checked out
              return RedirectToAction("Index");
          }
          else
          {
              var bookloan = (from book in _context.Books.Where(b =&gt; b.BookId == id)
                          join loan in _context.BookLoans.Where(x =&gt; !x.ReturnedOn.HasValue) on book.BookId equals loan.BookID into result
                          from loanWithDefault in result.DefaultIfEmpty()
                          select new BookReturnViewModel
                          {
                              BookLoanID = loanWithDefault.BookLoanID,
                              BookID = book.BookId,
                              Title = book.Title,
                              StudentID = loanWithDefault == null ? null : loanWithDefault.StudentID,
                              StudentFristName = loanWithDefault == null ? null : loanWithDefault.Student.FirstName,
                              StudentLastName = loanWithDefault == null ? null : loanWithDefault.Student.LastName,
                              //Fines
                              CheckedOutOn = loanWithDefault == null ? (DateTime?)null : loanWithDefault.CheckedOutOn,
                              IsAvailable = loanWithDefault == null,
                              AvailableOn = loanWithDefault == null ? (DateTime?)null : loanWithDefault.DueOn
                          }).FirstOrDefault();
  
              if (bookloan == null)
              {
                  return NotFound();
              }
  
              return View(bookloan);
          }
      }
  </code></pre>
  
  <p>Post:</p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; Return(BookReturnViewModel model)
      {
  
  
          if (ModelState.IsValid &amp;&amp; isBookCheckedOut(1) == true)
          {
              var bookloan = new BookLoan()
              {
                  BookLoanID = model.BookLoanID,
                  BookID = model.BookID,
                  StudentID = model.StudentID,
                  CheckedOutOn = (DateTime)model.CheckedOutOn,
                  DueOn = (DateTime)model.AvailableOn,
                  ReturnedOn = DateTime.Now
              };
  
  
              try
              {
  
                  _context.Update(bookloan);
                  await _context.SaveChangesAsync();
              }
              catch (DbUpdateConcurrencyException)
              {
  
              }
  
              return RedirectToAction("Index");
          }
          else
          {
  
          }
  
          return View();
      }
  </code></pre>