2
我想在用戶中添加和編輯項目。我用這個代碼:無法定義兩個對象之間的關係
DataContext db = new DataContext();
private void Save()
{
User user = (SelectedUser == null) ? new User() : db.User.Find(SelectedUser.UserName);
user.FirstName = FirstName;
user.CcRowIndex = CcRowIndex;
user.Image = Image;
user.LastName = LastName;
user.OrganizationalPostId = OrganizationalPost;
user.OrganizationalUnitId = OrganizationalUnit;
user.Password = Password;
user.Signature = Signature;
user.SubsetUsers = SubsetUsersList;
user.UserName = UserName;
if (SelectedUser == null)
{
db.User.Add(user);
}
db.SaveChanges();
}
我添加項目,但是當我編輯的項目,我得到錯誤:
The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.+ savechange of datacontext
[EF4錯誤:兩個對象之間的關係無法定義,因爲它們連接到不同的ObjectContext對象](http://stackoverflow.com/questions/4046843/ef4-errorthe-relationship-between-the - 兩個對象-不能待定義-因爲-的) –