none
Error : Collection was modified; enumeration operation may not execute. RRS feed

  • Question

  • Hello !

    I'm using EF6 , VB.net 2013.

    I'm trying to implement a way to copy an object to a new object.

    This is my code :

    <Extension> _
        Public Sub CopyTo(Of T)(copyFrom As T, copyTo__1 As T, copyParentProperties As Boolean)
            Dim props As PropertyInfo()
            If copyParentProperties Then
                props = GetType(T).GetProperties()
            Else
                props = GetType(T).GetProperties(BindingFlags.[Public] Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
            End If
            Dim i As Integer = 0
            While i < props.Length
                Dim propertyValue = copyFrom.[GetType]().GetProperty(props(i).Name).GetValue(copyFrom, Nothing)
                copyTo__1.[GetType]().GetProperty(props(i).Name).SetValue(copyTo__1, propertyValue, Nothing)
                System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
            End While
        End Sub

    But when I try to use the code like this :

    newitm = New Myobject
    currentobj.CopyTo(newitm, False)
    context.MyObjects.Add(Newitm)
    context.savechanges

    I get this error , on the line----- context.MyObjects.Add(Newitm) -------

    An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll
     
    Additional information: Collection was modified; enumeration operation may not execute.

    (But of course , if i create a new object and fill the properties manually one by one , the ADD method works without problems. )

    What may be the problem ?
    Thank you !



    • Edited by dcode25 Monday, April 13, 2015 2:35 AM
    Monday, April 13, 2015 2:02 AM

All replies

  • Hello,

    >>What may be the problem ?

    There are some unclear points are:

    Where do you add your extension method?

    What is the currentobj object and how do you declare it and assign valves to it?

    It is also confused your call the “currentobj.CopyTo(newitm, False)” while you provided an extension method which accepts three parameters().

    Based on your provided code, I created a demo and it could work with the extension method:

    Sub Main()
    
            Dim context As WinFormDBEntities = New WinFormDBEntities
    
            Dim order As Order = New Order() With {.ID = 1, .Name = "1"}
    
            Dim copiedOrder As Order = New Order
    
            order.CopyTo(order, copiedOrder, False)
    
            context.Orders.Add(copiedOrder)
    
            context.SaveChanges()
    
        End Sub
    

    You could check it to see if this would also work on your side.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 14, 2015 9:38 AM
    Moderator
  • Hello !

    Your code works , because you create a new object  , and immediately copy this.

    My CurrentObj is an existing object that I have read like this :

    MyBindingSource.Datasource=(For t in context.MyObjects select t).Tolist

    .....

    CurrentObj = MyBindingsource.current

    .......

    and as for the extension , is declared on a module , and I call with only 2 parameters ( the new object and the Boolean parameter ). But I have no problem on this call. the problem is when I try to add the newitm . 

    Tuesday, April 14, 2015 6:13 PM
  • Hello,

    It seems to be BindingSource object in window form, with your code, I also created a test as:

    Imports System.Reflection
    
    Imports System.Runtime.CompilerServices
    
    Imports System.Windows.Forms
    
    
    Module Module1
    
    
    
        Sub Main()
    
            Dim MyBindingSource As BindingSource = New BindingSource
    
    
            Dim context As WinFormDBEntities = New WinFormDBEntities
    
            MyBindingSource.DataSource = (From o In context.Orders).ToList
    
    
    
            Dim order As Order = New Order()
    
            Dim copiedOrder As Order = MyBindingSource.Current
    
            order.CopyTo(copiedOrder, order, False)
    
            context.Orders.Add(copiedOrder)
    
            context.SaveChanges()
    
        End Sub-
    
    
    End Module
    

    It however still works, I do not know what exactly operation you have done for the bindingsource object, if possible, please share more code with us.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 16, 2015 9:32 AM
    Moderator