2016-10-18 80 views
1

我得到的错误: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!

为什么我得到那个错误?

+0

你是否100%确定没有'瞳孔'被加载? “pupilIds”从哪里来?你有没有新的上下文实例? –

+0

不从数据库加载学生。我也试过context.entry(新学生(id = id).state = state.Deleted并做了context.savechanges()就像在EF6中,但也造成了一个例外... – Pascal

回答

2

删除记录需要从上下文进行跟踪。因此,而不是创造新Pupil收集从上下文

var pupilsToDelete = context.Pupils.Where(a => pupilIds.Contains(a.Id)).Select(b => b); 
context.Pupils.RemoveRange(pupilsToDelete); 
await context.SaveChangesAsync(); 
+0

该代码没有任何意义,甚至没有编译;-)用.Where()替换.Select()。让我们从那里开始。 – Pascal

+0

为什么不编译你的表名是不是'学生'?您正试图删除条目? – Mostafiz

+0

是的,我知道了,现在好了,现在检查,这是一个错误 – Mostafiz

0

只是引用它们要能够删除记录,你需要确保你的ObjectContext的是跟踪他们。删除项目的最佳方法是由@Mostafiz回答,但您也可以将您的实体附加到上下文。

var pupilsToDelete = pupilIds.Select(id => new Pupil { Id = id }); 
foreach(var p in pupilsToDelete) 
    context.Attach(p); 
context.RemoveRange(pupilsToDelete.ToList()); 
await context.SaveChangesAsync(); 

哪个不好办法。

+0

为什么它不是一个好方法? – Pascal

+1

Btw。有一个context.AttachRange(pupilsToDelete)...当我运行你的代码时,我在RemoveRange得到一个异常...似乎我遇到了一个bug ... – Pascal

+0

我从来没有尝试过,但听说它很慢。在Entity框架文档中,您可以阅读RemoveRange函数描述以及他们如何使用它。另外附加不能更好,因为无论如何它需要找到所有ID。 –

相关问题