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) – 2010-04-20 05:05:29