none
Exception from ObjectContext.SaveChanges() RRS feed

  • Question

  • I have some data to add to Entity Framework and then save to db. I got a strange exception when calling SaveChanges function

     -  e {"Collection was modified; enumeration operation may not execute."} System.Exception {System.InvalidOperationException}

    My code is listed below. What code be possible reasons for it?

    MyData myData = new MyData()

    // set myData here...

    _db.AddToMyDataCollection(_myData);

    _db.SaveChanges()  // this line generate the exception


    Roy
    Tuesday, August 9, 2011 8:31 PM

Answers

  • Hi Roy;

    Microsoft has Deprecated that Method for adding a new object and recommends the table add methods be used. I believe that in your code it should be the following, could you try that and see if that helps.

    _db.MyDataCollection.Add(_myData);

    Also are you modifying any of the objects after you add them to the ObjectContext but before doing a SaveChanges?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, August 10, 2011 4:19 PM

All replies

  • Roy,

    Is MyDataCollection the entity set name? And MyData is the entity type name? 

    Could you please email me a sample project so I can look at all the code.

    My email is: juliako@microsoft.com

     

    Thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, August 9, 2011 10:12 PM
    Moderator
  • Hi Roy;

    if you are adding myData to the object context to insert the record into the table then this command

    _db.AddToMyDataCollection(_myData);

    Should be something like this:

    ObjectContextInstance.Tablename.AddObject(myData);

    Then execute the SaveChanges method on the object context.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, August 10, 2011 3:50 AM
  • Hi Roy,

    Welcome!

    I guess there may some foreach loops in your code, when you retrieve collection from database, you should use ToList<T> after your query, then loop the colletion. 

    Would you please give us more information, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, August 10, 2011 8:44 AM
    Moderator
  • This is the place where the error message confuses me. I donot have a foreach loop. I have a table called MyData and the edmx set for it is called MyDataCollection. MyData contains a varbinary called MyBinaryData and some other regular fields. When the function is called, I do the following:

    (_myData is allocated at initailization time)

    lock(_db)

    {

    _myData.MyBinaryData = value; // where value is a byte[]

    // set other fields here

    _db.AddToMyDataCollection(_myData);

    _db.SaveChanges();

    _db.Detach(_myData)

    _myData = new MyData; // create a new MyData for next function call

    }

    This function is owned by a class and is called by another function and a Timer. And I have hundreds of such class objects running together. They share the same ObjectContext _db.


    Roy
    Wednesday, August 10, 2011 1:26 PM
  • Hi Roy;

    In your response you state, "table called MyData and the edmx set for it is called MyDataCollection", if in fact you have an edmx you have a Linq to Sql and NOT a Linq To Entity Framework model. In that case you have posted the question to the wrong thread. Linq to SQL questions should be posted to http://social.msdn.microsoft.com/Forums/en-US/linqtosql/threads.

    Sorry the above is incorrect this is a Entity Framework model.

    Now to try to get a solution to the question. You have a function that seems to be added to the DataContext class called AddToMyDataCollection, can you post that code so that we can have a look at it.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, August 10, 2011 2:14 PM
  • This is the function being generated when you use wizard to generate edmx classes from database. For each table, the wizard will add a function to your ObjectContext specific class for your type. I donot add them myself.

     

     

    ///

    <summary>

     

    ///

    There are no comments for MyDataCollection in the schema.

     

    ///

    </summary>

    [

    global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0"

    )]

     

    public void AddToMyDataCollection(MyData

    myData)

    {

     

    base.AddObject("MyDataCollection"

    , myData);

    }


    Roy
    Wednesday, August 10, 2011 3:30 PM
  • (_myData is allocated at initailization time)

    lock(_db)

    {

    _myData.MyBinaryData = value; // where value is a byte[]

    // set other fields here

    _db.AddToMyDataCollection(_myData);

    _db.SaveChanges();

    _db.Detach(_myData)

    _myData = new MyData; // create a new MyData for next function call

    }

    This function is owned by a class and is called by another function and a Timer. And I have hundreds of such class objects running together. They share the same ObjectContext _db.

    I have some data to add to Entity Framework and then save to db. I got a strange exception when calling SaveChanges function

     -  e {"Collection was modified; enumeration operation may not execute."} System.Exception {System.InvalidOperationException}

    The error message confuses me. I donot have a foreach loop. I have a table called MyData and the edmx set for it is called MyDataCollection. MyData contains a varbinary called MyBinaryData and some other regular fields. When the function is called, I do the following:


    Roy
    Wednesday, August 10, 2011 3:47 PM
  • Hi Roy;

    Microsoft has Deprecated that Method for adding a new object and recommends the table add methods be used. I believe that in your code it should be the following, could you try that and see if that helps.

    _db.MyDataCollection.Add(_myData);

    Also are you modifying any of the objects after you add them to the ObjectContext but before doing a SaveChanges?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, August 10, 2011 4:19 PM
  • Cross post located here.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/6ca238f0-86be-4155-b3c8-bf91b58f1824/#c000619e-1056-48a9-82b9-cf900d72bd0a


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, August 10, 2011 4:21 PM
  • Hi Roy,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 12, 2011 9:53 AM
    Moderator
  • From the code you posted, I can see the 'lock'. May I assume you are using multi-thread or timer? If so, Entity framework is not thread-safe. You need a better way to handle it. Lets say "Delegate" or "Using". Hopefully it helps.
    Tuesday, November 15, 2011 4:55 AM