我得到的错误:RemoveRange抛出InvalidOperationException异常实体框架的核心
The instance of entity type 'Pupil' cannot be tracked because another instance of this type with the same key is already being tracked. When adding new entities, for most key types a unique temporary key value will be created if no key is set (i.e. if the key property is assigned the default value for its type). If you are explicitly setting key values for new entities, ensure they do not collide with existing entities or temporary values generated for other new entities. When attaching existing entities, ensure that only one entity instance with a given key value is attached to the context.
我能明白这一点错误,如果我想这种情况下,然后在上下文高速缓存加载之前已经恢复,但我haven`t! !
var pupilsToDelete = pupilIds.Select(id => new Pupil { Id = id });
context.RemoveRange(pupilsToDelete.ToList());
await context.SaveChangesAsync();
运行期间pupilIds都是不同的ID!
为什么我得到那个错误?
你是否100%确定没有'瞳孔'被加载? “pupilIds”从哪里来?你有没有新的上下文实例? –
不从数据库加载学生。我也试过context.entry(新学生(id = id).state = state.Deleted并做了context.savechanges()就像在EF6中,但也造成了一个例外... – Pascal