0
이 예외가 발생 함 - 두 객체 간의 관계는 서로 다른 ObjectContext 객체에 연결되어 있으므로 정의 할 수 없습니다. . "왜이 예외입니까? - 두 객체 간의 관계는 다른 ObjectContext 객체에 연결되어 있기 때문에 정의 할 수 없습니다.
사용자 테이블과 국가 테이블. countryid는 사용자 테이블에서 참조됩니다.
사용자 테이블에 항목을 추가하려고 할 때 위의 예외가 발생합니다.
이 내 코드 - 다음 관계를 추가, 먼저 사용자를 추가
using (MyContext _db = new MyContext())
{
User user = User .CreateUser(0, Name, address, city, 0, 0, email, zip);
Country country = _db.Country.Where("[email protected]", new ObjectParameter("Id",countryId)).First();
user.Country = country;
State state = _db.State.Where("[email protected]", new ObjectParameter("Id", stateId)).First();
user.State = state;
_db.AddToUser(user);//Here I am getting that Exception
_db.SaveChanges();
}
현재 Where 절은 _db.Country.Where (c => c.Id == countryId)로 단순화 할 수 있습니다. –