Questions tagged «entity-framework-core-3.1»

1
尝试通过属性默认值更改关系时发生意外的InvalidOperationException
在下面的示例代码中,执行此操作时出现以下异常db.Entry(a).Collection(x => x.S).IsModified = true: System.InvalidOperationException:'无法跟踪实体类型'B'的实例,因为已经跟踪了具有键值'{Id:0}'的另一个实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。 为什么不添加而不是附加B的实例? 奇怪的是,文档IsModified未指定InvalidOperationException可能的例外。无效的文档或错误? 我知道这段代码很奇怪,但是我写它只是为了了解ef core在某些奇怪的egde情况下是如何工作的。我想要的是一个解释,而不是变通的方法。 using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; class Program { public class A { public int Id { get; set; } public ICollection<B> S { get; set; } = new List<B>() { new B {}, new B …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.