title: Another instance of this type with the same key is already being tracked
body: <p>I faced with this error <code>The instance of entity type  cannot be tracked because another instance of this type with the same key is already being tracked</code> when saving model in ASP.NET Core.</p>
  
  <p>Quick solution not work: <strong>My DbContext isn't singleton</strong>. Also, I face with this error only on one model, saving other works correct.</p>
  
  <h3>This is my Repository saving method:</h3>
  
  <pre><code> public class EFRepository:IRepository
   {
      private DatabaseContext dbcontext;
  
      ...
  
      public void InsertOrUpdate&lt;T&gt;(T entity) where T : AbstractModel
      {
          costil_logging(entity);
  
          if (entity==null) throw new NullReferenceException();
  
          if(dbcontext.Set&lt;T&gt;().Any(x=&gt;x.ID==entity.ID))
          {
              //Update
              dbcontext.Entry(entity).State = EntityState.Modified; //ERROR HERE
          }
          else
          {
              entity.ID = 0;
              dbcontext.Entry(entity).State = EntityState.Added;
          }
  
          dbcontext.SaveChanges();
  
      }
  }
  </code></pre>
  
  <h3>This is my Repository initialization:</h3>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
    ...
    services.AddScoped&lt;IRepository, EFRepository&gt;();
    ...
  }
  </code></pre>
  
  <h3>This is my model:</h3>
  
  <pre><code>public class Section:AbstractModel
  {
     public string Name { get; set; }
     public bool IsMedicalSection { get; set; }
  
     public virtual ICollection&lt;Category&gt; Categories
      {
          get { return this.categories ?? (this.categories = new List&lt;Category&gt;()); }
          set { categories = value; }
      }
  
    public virtual ICollection&lt;PriceCategory&gt; PriceCategories
      {
          get { return this.priceCategories ?? (this.priceCategories = new List&lt;PriceCategory&gt;()); }
          set { priceCategories = value; }
      }
  </code></pre>
  
  <h3>And here is my controller save method:</h3>
  
  <pre><code>    [Route("section/edit")]
      [HttpPost]
      public IActionResult EditSave(Section section)
      {
          if (ModelState.IsValid)
          {
              repository.InsertOrUpdate(section);
              return RedirectToAction("Index", "Admin");
          }
  
          //Errors displaying
          return View("Edit",get_error_model(section));
      }
  </code></pre>
  
  <h2>What kind of problem it can be?</h2>