An entity object cannot be referenced by multiple instances of IEntityChangeTracker EF 6
<p>I am developing an application in MVC 5 and EF 6. I am using a generic method to update the entity and an extension method to detect the changed values and properties. When I try to insert updated record, I got the error,</p>

<blockquote>
  <p>An entity object cannot be referenced by multiple instances of IEntityChangeTracker</p>
</blockquote>

<p>My code for Update is:</p>

<pre><code>public void Update(T data)
{
    var originalEntity = db.Set&lt;T&gt;().AsNoTracking().FirstOrDefault(e =&gt; e.Id == data.Id);
    var modifiedValues = originalEntity.ModifiedValues&lt;T&gt;(data).ToList();
    for (int i = 0; i &lt; modifiedValues.Count; i++)
    {
       string modifiedField = modifiedValues.ElementAt(i).Key;
       string modifiedValue = modifiedValues.ElementAt(i).Value.ToString();
       PropertyInfo prop = data.GetType().GetProperty(modifiedField, BindingFlags.Public | BindingFlags.Instance);
       string s = prop.PropertyType.GenericTypeArguments.Select(x =&gt; x.FullName).SingleOrDefault();
       switch (s)
       {
            case ("System.DateTime"):
            {
                DateTime d = Convert.ToDateTime(modifiedValue);
                prop.SetValue(data, d);
                break;
            }
            case ("System.String"):
            {
                prop.SetValue(data, modifiedValue);
                break;
            }
       }
   }
   Insert(data); // error occurred in this function
   Save();    
}
</code></pre>

<p>The code for Insert function is:</p>

<pre><code>public void Insert(T data)
{ db.Set&lt;T&gt;().Add(data); }
</code></pre>

<p>at the statement <strong>db.Set().Add(data);</strong>, the error occurred.</p>

<p>I know this occurs when the entity is already attached to another Context. But in these functions I only use the context object <em>db</em> once but with <strong>AsNoTracking()</strong>, so this error shouldn't occur.</p>

<p>I looked to answer in <a href="https://stackoverflow.com/questions/14725831/an-entity-object-cannot-be-referenced-by-multiple-instances-of-ientitychangetrac">this question</a> and modify my Update function as:</p>

<pre><code>db.Entry(data).State = EntityState.Detached;
Insert(data); // error again occurred in this function
Save(); 
</code></pre>

<p>But the error still occurs. What is the problem? How can I solve it?</p>

<p>I call the Update function from my COntroller:</p>

<pre><code>private BaseRepository&lt;ABC&gt; g = new BaseRepository&lt;ABC&gt;();
ABC m = new ABC();
m = db.ABCs.Find(id);
m.S107 = "2";
m.D103 = DateTime.Now;
m.S36 = "awais";
g.Update(m);
</code></pre>