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 - 两个对象-不能待定义-因为-的) –