下面的 ActionResult 曾经工作,但最近停止工作。db.SaveChanges()
抛出一个异常,抱怨User
和Class
字段是必需的。但他们已经在那里了!如果我只是简单地设置User
和Class
已经存在的值......
item.User = item.User;
item.Class = item.Class;
然后异常消失。显然这里有一些我误解的大问题。如果这段代码失败,我应该如何更新记录的字段?
感谢洞察!!
public ActionResult ApproveChange(int id)
{
var item = db.EnrollmentItems.FirstOrDefault(x => x.ID == id);
if (item != null
&& item.State == EnrollmentState.Submitted
&& LoginUser.IsManager(ClientBusiness))
{
item.State = EnrollmentState.Approved;
item.LastTouch = DateTime.UtcNow;
db.SaveChanges();
return Json(new { State = item.State });
}
return Json(new { State = "error" });
}
这是类定义
public class EnrollmentItem
{
public int ID { get; set; }
[Required]
public virtual User User { get; set; }
[Required]
public virtual Class Class { get; set; }
public string Action { get; set; }
public string State { get; set; }
public DateTime LastTouch { get; set; }
public override string ToString()
{
return
String.Format("{0} {1} {2} {3} {4}",
User, Action, ID, State, Class);
}
}