title: Why is this Exception?- The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects
body: <p>I m getting this Exception-"The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects."</p>
  
  <p>I ve user table and country table. The countryid is referred in user table.</p>
  
  <p>I am getting the above Exception when I am trying to add entry in user table.</p>
  
  <p>This is my code-</p>
  
  <pre><code>using (MyContext _db = new MyContext ())    
  {                
      User user = User .CreateUser(0, Name, address, city, 0, 0, email, zip); 
  
      Country country = _db.Country.Where("it.Id=@Id", new ObjectParameter("Id",countryId)).First(); 
  
      user.Country = country;
  
      State state = _db.State.Where("it.Id=@Id", new ObjectParameter("Id", stateId)).First(); 
  
      user.State = state;
  
      _db.AddToUser(user );//Here I am getting that Exception
  
      _db.SaveChanges();    
  }
  </code></pre>