title: Can't find objects which reference entity framework entity
body: <p>I'm running into an InvalidOperationException because "An entity object cannot be referenced by multiple instances of IEntityChangeTracker." on the first line of EntityFrameWorkRepository.Create().</p>
  
  <p>I know this is due to having multiple database contexts, but in this case I'm a bit lost as the code has no obvious second context since all database access goes through a designated object whose sole purpose is managing database contexts. This was done as the web application in question is fairly interactive and so the user is constantly creating new objects which must be saved in the database. This was causing issues with the previous design, which used locking and a single context, thus the code was refactored and works, except for the method in question.</p>
  
  <p><strong>EF class:</strong></p>
  
  <pre><code>public class EntityFrameWorkRepository&lt;TKey, TEntity&gt; : IDisposable, IRepository&lt;TKey,TEntity&gt; where TEntity: class
  {
      private readonly IDbContext _context;
      private IDbSet&lt;TEntity&gt; _entities;
  
      public EntityFrameWorkRepository()
      {
          _context = new ApplicationDbContext();
      }
  
      private IDbSet&lt;TEntity&gt; Entities
      {
          get { return _entities ?? (_entities = _context.Set&lt;TEntity&gt;()); }
      }
  
      public void Create(TEntity entity)
      {
          Entities.Add(entity);
          _context.SaveChanges(); 
      }
  
      public void Dispose()
      {
          _context.Dispose();
      }
  }
  </code></pre>
  
  <p>The service object used for all DB access:</p>
  
  <pre><code>public class Service : IService
  {
      public const string Persistance = "Persist";
      public const int CacheTaskSeconds = 300; //Check every 5 minutes
      public const double IdleMinutes = 30.0; 
  
      private readonly IKvpRepository&lt;int, SimulationCollection&gt; _simulationCollectionAppStateRepository;
      private readonly UserManager&lt;ApplicationUser&gt; _userManager;
  
      public Service(IKvpRepository&lt;int, SimulationCollection&gt; simulationCollectionAppStateRepository)
      {
          _userManager = new UserManager&lt;ApplicationUser&gt;(new UserStore&lt;ApplicationUser&gt;(new ApplicationDbContext()));
          AddTaskToCache(Persistance, CacheTaskSeconds);
      }
  
      public SimulationCollection CreateCollection(Guid userId, string name, string description)
      {
          using (var _simulationCollectionEFRepository = new EntityFrameWorkRepository&lt;int, SimulationCollectionEntity&gt;())
          {
              var applicationUser = _userManager.FindById(userId.ToString());
              if (applicationUser == null)
                  throw new ArgumentOutOfRangeException("ApplicationUser matching userId doesn't exist");
              var collectionEntity = new SimulationCollectionEntity(applicationUser, name, description);
              _simulationCollectionEFRepository.Create(collectionEntity);
              return collection; 
          }
      }
  }
  </code></pre>
  
  <p>The object I'm trying to add to the database:</p>
  
  <pre><code>public class SimulationCollectionEntity
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public virtual int Id { get; set; }
      public string Name { get; set; }
      public virtual ApplicationUser User { get; set; }
      public DateTime DateCreated { get; set; }
      public string Description { get; set; }
  
      [ForeignKey("SimulationCollectionEntityId")]
      public virtual ICollection&lt;SimulationEntity&gt; Simulations { get; set; }
  
      [Obsolete("Only needed for serialization and materialization", true)]
      public SimulationCollectionEntity() {}
  
      public SimulationCollectionEntity(ApplicationUser currentUser, string name, string description)
      {
          User = currentUser;
          Name = name;
          Description = description;
          DateCreated = DateTime.Now;
      }
  }
  </code></pre>
  
  <p>Is there an easy way to view what contexts a given object might be attached to? I already checked to see if collectionEntity is attached to _userManager since it has a dbContext, but its state is detached. Does EF maybe expect me to add objects in a different way than I am? I suspect that the attributes in SimulationCollectionEntity might be causing me trouble but I'm new to Entity Framework and I'm not sure. Should I maybe be going for a different design instead like <a href="https://stackoverflow.com/a/6334865/158703">this</a>?</p>