我有这些课程:
public class Project
{
public Guid Id { get; set; }
public guid ProjectTypeId{ get; set; }
public string projectType{ get; set; }
[Required]
public ApplicationUser User { get; set; }
[MaxLength(125)]
public string UserId { get; set; }
[Required]
public DateTime CreationDate{ get; set; }
//more properties here
//...
public bool Activated{ get; set; }
public IList<ProjectDetail> ProjectDetails{ get; set; }
}
一个项目有很多细节:
public class ProjectDetail
{
[Required]
public Guid Id { get; set; }
public Project Project{ get; set; }
[Required]
public Guid ProjectId{ get; set; }
public string DetailDescription{ get; set; }
public IList<ProjectDetailsAnswer> ProjectDetailsAnswers{ get; set; }
}
我想做的是只更新记录中的一个字段
var ProjectInDb = _dbContext.Project.Where(.....).FirstOrDefault();
if (ProjectInDb == null)
return false;
ProjectInDb.Activated = true;
ProjectInDb.DetailDescription = "updating this";
var x = _dbContext.SaveChanges();
但是,当我执行_dbContext.SaveChanges()
时,会出现错误
“用户”字段为必填项。
我尝试使用此方法修复
_dbContext.Configuration.ValidateOnSaveEnabled = false;
但是由于某种原因,我的代码中存在一个错误,该错误有时会更新该字段,而有时却不会。
我该如何解决错误>“用户字段”为必填项??
谢谢