title: I get the error : “An entity object cannot be referenced by multiple instances of IEntityChangeTracker.” with .net MVC2 and the Entity Framework4
body: <p>I have a big problem since some days and I’m a very beginner in the Entity Framework.
  I have 2 entities : Group and News. A news is visible by one or many groups. I use two repositories (newsRepository and groupsRepository).</p>
  
  <p>This is my Create method for the news :  </p>
  
  <pre><code>    public ActionResult Create()
      {
          return View(new CreateNewsViewModel(new News()));
      }
  
      [HttpPost]
      public ActionResult Create(CreateNewsViewModel model)
      {
          model.news.CategoryId = Int32.Parse(Request.Form["news.CategoryId"]);
  
          if (ModelState.IsValid)
          {
              News news = new News();
  
              DateTime date = DateTime.Now;
  
              //AuthorId a recuperer
              news.AuthorId = 1;
              news.Title = IntranetTools.UppercaseFirst(model.news.Title.Trim());
              news.Content = model.news.Content;
              news.IsVisible = Request.Form["news.IsVisible"].Contains("true");
              news.CreateDate = date;
              news.PublicationDate = date;
              news.LastChangedDate = date;
              news.CategoryId = model.news.CategoryId;
  
              // Collection des groupes concernés
              foreach (var c in model.allGroups)
              {
                  if (Request.Form["" + c.GroupId].Contains("true"))
                  {
                      News.Groups.Add(c);
                  }
              }
  
              _newsRepository.AddToNewsSet(news);
              _newsRepository.SaveChanges();
  
              return Redirect("/NewsAdmin/Index/");
          }
          return View(model);
      }
  </code></pre>
  
  <p>I say that all my groups are already created. I just want to insert the groups (chosen by the user via checkboxes). In my “CreateNewsViewModel”, I create a list of groups that contains all existing groups in my DB. I get the list in my view, via a “foreach” loop and create a checkbox for each group.</p>
  
  <p>I reuse the same list in my controller to compare if checkboxes have been checked. 
  For each “true” value, I add groups in the groups collection of my news (just created).</p>
  
  <p>With this, I get this Error Message :</p>
  
  <p><strong>“An entity object cannot be referenced by multiple instances of IEntityChangeTracker.”</strong> (at line _newsRepository.AddToNewsSet(news);)</p>
  
  <p>I try some solutions but I still don’t understand how I can solve this problem.</p>
  
  <p>Thanks for all</p>
  
  <hr>
  
  <p><strong>Edit</strong> </p>
  
  <p>Actually, if I use explicitly two contexts and detach/attach my objects to other context it's fine and I have no erros.</p>
  
  <pre><code>            ObjectContext context  = _newsRepository.Context;
              ObjectContext context2 = _groupsRepository.Context;
  
              foreach (var c in groups)
              {
                  if (Request.Form["" + c.GroupId].Contains("true"))
                  {
                      context2.Detach(c);
                      context.Attach(c);
                      news.Groups.Add(c);
                  }
              }
  </code></pre>
  
  <p>I would like to use the Ladislav Mrnka's solution and use the dependency injection (I use Ninject framework) to give the same ObjectContext to my repositories (in single request processing).
  I understand the concept but I don't know how to code it.</p>