title: ASP.NET MVC, EF and Ninject : Different ObjectContext issue
body: <p>I'm using Ninject for DI and injecting IDbContext to my repositories as constructor parameter. I get a <strong><em>"The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects."</em></strong> error while trying to do something like this:</p>
  
  <p>This is my controller's <strong>action method</strong>:</p>
  
  <pre><code> public ActionResult BindSpace(int spaceId, int managerId)
      {
          Space space = _spaceService.GetSpace(spaceId);
          Manager manager = _managerService.GetManager(managerId);
  
          if (space != null &amp;&amp; manager != null)
          {
              _spaceService.BindManager(space, manager);
          }
  
          return RedirectToAction("GetSpaceBindingForm", new { id = space.Id });
      }
  </code></pre>
  
  <p>This is the <strong>service method</strong>:</p>
  
  <pre><code>public void BindManager(Space space, Manager manager)
      {
          if (space != null &amp;&amp; manager != null)
          {
              space.Managers.Add(manager);
              _spaceRepo.Update(space);
          }
      }
  </code></pre>
  
  <p>There was no problem while adding and updating non-related entities. </p>
  
  <p>There is no problem when I use:</p>
  
  <p><code>ninjectKernel.Bind&lt;IDbContext&gt;().To&lt;SPBSObjectContext&gt;().InSingletonScope().WithConstructorArgument("nameOrConnectionString", "ShoppingPointBrowsingSystem");</code></p>
  
  <p>I searched the web and everyone implements and uses NinjectModule abstract base class, but I have the following code. What am I doing wrong here?</p>
  
  <p>This is the <strong>injection</strong> part:</p>
  
  <pre><code>public class NinjectControllerFactory : DefaultControllerFactory
  {
      private IKernel ninjectKernel;
  
      public NinjectControllerFactory()
      {
          ninjectKernel = new StandardKernel();
          AddBindings();
      }
  
      protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
      {
          return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
      }
  
      private void AddBindings()
      {
          // HTTP Context
          ninjectKernel.Bind&lt;HttpContextBase&gt;().ToMethod(context =&gt; new HttpContextWrapper(HttpContext.Current));
  
          // Context
          ninjectKernel.Bind&lt;IDbContext&gt;().To&lt;SPBSObjectContext&gt;().InRequestScope().WithConstructorArgument("nameOrConnectionString", "ShoppingPointBrowsingSystem");
  
          // Repositories
          ninjectKernel.Bind&lt;IRepository&lt;Admin&gt;&gt;().To&lt;EfRepository&lt;Admin&gt;&gt;().InRequestScope();
          ninjectKernel.Bind&lt;IRepository&lt;Manager&gt;&gt;().To&lt;EfRepository&lt;Manager&gt;&gt;().InRequestScope();
          ninjectKernel.Bind&lt;IRepository&lt;ShoppingCenterSpace&gt;&gt;().To&lt;EfRepository&lt;ShoppingCenterSpace&gt;&gt;().InRequestScope();
          ninjectKernel.Bind&lt;IRepository&lt;IndependentStoreSpace&gt;&gt;().To&lt;EfRepository&lt;IndependentStoreSpace&gt;&gt;().InRequestScope();
          ninjectKernel.Bind&lt;IRepository&lt;Space&gt;&gt;().To&lt;EfRepository&lt;Space&gt;&gt;().InRequestScope();
  
          // Services
          ninjectKernel.Bind&lt;IAuthenticationService&lt;Admin&gt;&gt;().To&lt;AdminFormsAuthenticationService&gt;();
          ninjectKernel.Bind&lt;IAdminService&gt;().To&lt;AdminService&gt;();
          ninjectKernel.Bind&lt;IManagerService&gt;().To&lt;ManagerService&gt;();
          ninjectKernel.Bind&lt;IShoppingCenterSpaceService&gt;().To&lt;ShoppingCenterSpaceService&gt;();
          ninjectKernel.Bind&lt;IIndependentStoreSpaceService&gt;().To&lt;IndependentStoreSpaceService&gt;();
          ninjectKernel.Bind&lt;ISpaceService&gt;().To&lt;SpaceService&gt;();
      }
  }
  </code></pre>