首页 > 解决方案 > 实体框架:由于验证问题无法更新记录

问题描述

我有这些课程:

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; 

但由于某种原因,在我的代码中,我有一个错误,有时会更新字段,有时不会..

如何解决错误>需要用户字段。?

谢谢

标签: c#asp.net-mvcentity-framework

解决方案


推荐阅读