假设以下方法存在于 WCF 服务中。UI 检索到 Status 对象的实例,并使用此方法对服务进行后续调用。它没有像我期望的那样将状态分配给用户,而是尝试插入状态。我究竟做错了什么?
void Method(Status status)
{
//not sure if this is even needed, the status never changed
context.Statuses.ApplyChanges(status);
//get the first user from the database
User user = context.Users.Where(u => u.Id = 1).First();
//set user status to some existing status
user.Status = status;
//this throws an exception due to EF trying to insert a new entry
//into the status table, rather than updating the user.StatusId column.
context.SaveChanges();
}