title: Can't attach entity to Database Context
body: <p>We are using EntityFramework on our project. I have the following code:</p>
  
  <pre><code>var realType = ObjectContext.GetObjectType(entity.GetType());
      if (this.Entry(entity).State == EntityState.Detached)
      {   
          this.Set(realType).Attach(entity);
      }
  </code></pre>
  
  <p>inside my Database Context class which throws an exception: </p>
  
  <p><code>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.</code></p>
  
  <p>Is why does it happens after I checked that entity was detached? How can I fix that?</p>