c# - 实体框架:由于验证问题无法更新记录
问题描述
我有这些课程:
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;
但由于某种原因,在我的代码中,我有一个错误,有时会更新字段,有时不会..
如何解决错误>需要用户字段。?
谢谢
解决方案
推荐阅读
- sql - 在 linq to sql 查询中使用 contains() 获取数据
- swift - 用于单元测试的 UserDefaults 实例?
- python - 如何在 Python 中使用 Bokeh 查看选择的 Holoviews
- javascript - 如何计算所有嵌套对象的相同值的计数?
- node.js - 开发电子 + Vue 应用程序时,RaspberryPi 上的 i2c-bus 绑定错误
- google-sheets - Google 表格 - 如何获取一个值并将其连接到另一个单元格?
- azure - 如何在 Azure 中获取 Google 分析数据
- git - 推送递归子模块 - 上传包:不是我们的参考
- python - 保留级联 ttk.combobox 的索引
- python - Python(Redis + DRF):类型无效的输入:'NoneType'。先转换为字节、字符串、整数或浮点数