我想在用户中添加和编辑项目。我使用这段代码:
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();
}
我添加了项目,但是当我编辑项目时出现错误:
无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象。+ datacontext的savechange