我的模型PaymentRequest
如下:
public class PaymentRequest
{
[Key]
public int Id { get; set; }
.....
.....
[DefaultValue(PaymentStatus.WaitForPayment)]
public PaymentStatus Status { get; set; }
[Required]
public virtual ApplicationUser RequestedBy { get; set; }
[ForeignKey("RequestedBy")]
public string RequestedById { get; set; }
}
这里RequestedById
是外键。当我想更新模型的Status
字段时,如下所示:
PaymentRequest paymentrequest = db.PaymentRequests.Find(id);
if (paymentrequest == null)
{
return HttpNotFound();
}
db.Entry(paymentrequest).State = System.Data.Entity.EntityState.Modified;
paymentrequest.Status = PaymentRequest.PaymentStatus.WaitForPayment;
db.SaveChanges();
当我检查调试模式时,我面临有关RequestedBy
的必需属性的验证错误。但是,当我手动设置ID字段如下所示,问题解决:
PaymentRequest paymentrequest = db.PaymentRequests.Find(id);
if (paymentrequest == null || paymentrequest.PaymentType != PaymentRequest.PaymentRequestType.Tankhah)
{
return HttpNotFound();
}
db.Entry(paymentrequest).State = System.Data.Entity.EntityState.Modified;
paymentrequest.Status = PaymentRequest.PaymentStatus.WaitForPayment;
string uid = paymentrequest.RequestedById;// <-- fix lines
paymentrequest.RequestedById = uid;// <-- fix lines
db.SaveChanges();
我想知道导致验证错误的错误是什么?是关于virtual
属性及其延迟加载行为还是我错过了其他内容?