entity-framework - EF Core 2.1 更新相关数据
问题描述
我的编辑控制器
public async Task<IActionResult> Put(long id, [FromBody] Student student)
{
var p = await _Context.Students
.Include(t => t.PhoneNumbers)
.Include(t => t.StudentCourses)
.SingleOrDefaultAsync(t => t.Id == id);
if (p == null)
{
return NotFound();
}
p.PhoneNumbers = student.PhoneNumbers;
p.StudentCourses = student.StudentCourses;
_Context.Update(p);
await _Context.SaveChangesAsync();
return NoContent();
}
当我尝试更新相关实体时,我收到以下错误
System.InvalidOperationException: The instance of entity type 'PhoneNumber' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
我是否以错误的方式进行更新。请帮我纠正它。我也不确定 Direct _context.update() 和 _context.students.update() 之间的区别。请赐教。
解决方案
推荐阅读
- mysql - 用wordpress设置mysql
- python - 识别 HTTP 请求中的参数
- python - 注释掉“for”语句导致 'NoneType' 没有 len()
- vb.net - 带有响应的 HttpWebRequest
- android-studio - 任何修改后需要 CLEAN PROJECT 的未知错误
- sql - Amazon Redshift 中的递归 CTE
- amazon-web-services - 使用 S3 作为 AWS DMS 的目标:上传的文件名不会改变
- python-3.x - 如何从日期 x 到日期 y 获取推文
- ios - 这是在闭包中使用弱自我的正确位置吗?
- angular - 角2形式
未检测到更改事件