简体   繁体   English

将实体附加到数据库上下文时实例化枚举器后修改了集合

[英]Collection was modified after the enumerator was instantiated when attaching an entity to database context

To attach an entity to database context in entity framework, I use the following:要将实体附加到实体框架中的数据库上下文,我使用以下内容:

var student = GetStudent();
if (!_dbContext.Students.Local.Any(e => e.ID == student.ID))
    _dbContext.Attach(student);

This code sometimes (eg, 1 out of 3) fails with the following error raised when Attach is called:此代码有时(例如,3 个中的 1 个)会失败,并在调用Attach时引发以下错误:

System.InvalidOperationException: Collection was modified after the enumerator was instantiated. System.InvalidOperationException:枚举数实例化后修改了集合。

Simply re-running this piece of code when exception is thrown, may resolve the issue.抛出异常时只需重新运行这段代码即可解决问题。 However, I interested in knowing why I see this error, what causes it, and how I can resolve it?但是,我很想知道为什么会看到此错误,是什么原因造成的,以及如何解决它?

(This code belongs to a big project, and I am not sure which parts can help identify the cause of this issue; if you suspect anything, let me know and will share related code.) (此代码属于一个大项目,我不确定哪些部分可以帮助确定此问题的原因;如果您有任何疑问,请告诉我,并将分享相关代码。)

If you are re-running the project and it is working as expected i assume this might be related to caching.如果您正在重新运行该项目并且它按预期工作,我认为这可能与缓存有关。 Could you please add AsNoTracking() method like this.您能否像这样添加 AsNoTracking() 方法。

var student = GetStudent();
if (!_dbContext.Students.AsNoTracking().Local.Any(e => e.ID == student.ID))
    _dbContext.Attach(student);

I can't give you exact answer for this, but exception explains whats happening.我不能给你确切的答案,但例外解释了发生了什么。 In simple terms your collection is getting changed by other thread when you are trying to attach.简单来说,当您尝试附加时,您的收藏会被其他线程更改。

This usually happens when you are using parallel programming.这通常发生在您使用并行编程时。 By Debugging you have to identify whats happening underneath of your code and try to resolve it.通过调试,您必须确定代码下面发生的事情并尝试解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 枚举器被实例化后,集合被修改错误 - Collection was modified after the enumerator was instantiated error 奇怪的“集合在枚举器实例化后被修改”异常 - Strange “Collection was modified after the enumerator was instantiated” exception 枚举器在迁移中实例化后修改了集合 - Collection was modified after the enumerator was instantiated in migration Visual Studio崩溃了! - 枚举器实例化后修改了集合 - Visual Studio Crashes! - Collection was modified after the enumerator was instantiated 枚举器实例化后修改了集合--Request.ServerVariables - Collection was modified after the enumerator was instantiated - Request.ServerVariables 将现有但已修改的实体附加到上下文(数据库中的下拉列表) - Attaching an existing but modified entity to the context (Drop-down list from Database) 实体附加到上下文并且无法删除它 - Entity attaching to context and unable to remove it 将POCO实体附加到上下文时发生InvalidOperationException - InvalidOperationException on Attaching POCO entity to context 将状态设置为“已修改”时,为什么实体框架会引发附加实体错误? - Why does Entity Framework throw attaching entity errors when setting status to Modified? 将几个现有的修改实体附加到上下文中 - Attaching several existing modified entitities to the context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM