0

My entity framework DB context has this:

public DbSet<TableCell> TableCells { get; set; }

Table cell business logic class has this function for adding:

public void addTableCell(TableCell tc)
{
    context.TableCells.Add(tc);
    context.SaveChanges();
}

Cell entity is this:

[Table("TableCell")]
public class TableCell
{
    public TableCell()
    {
        this.TableElements = new HashSet<TableElement>();
        this.SomeObjects = new HashSet<SomeObject>();
    }

    [Key]
    public int PK_TableCellID { get; set; }

    [Required]
    public int Row { get; set; }

    [Required]
    public int Column { get; set; }

    [Required]
    [ForeignKey("Table")]
    public int FK_TableID { get; set; }

    public ICollection<TableElement> TableElements { get; set; }

    public ICollection<SomeObject> SomeObjects { get; set; }

    public Table Table{ get; set; }
}

I want to create a table, user will select the table size and I will create cells for users decision. Every cell will contain some elements. To do this, I have a method like this:

public void createTableWithCells()
{
    Table table = new Table
    {
        //row number added here with the user input
        //column number added here with the user input
        //I don't add any cell object here.
    };

    TableLogic.addTable(table);

    for (int row = 0; row < table.rowNumber; row++)
    {
        for (int column = 0; column < table.columnNumber; column++)
        {
            TableCell cell = new TableCell
            {
                //cell elements, properties etc.
                SomeObjects = someObjectList,
                Table = table
            };

            cellLogic.addTableCell(cell);
        }
    }
}

The problem is, for loops adds only one cell. After the first iteration, in second iteration when it comes to the line

cellLogic.addTableCell(cell);

I got an exception on cellLogic's add method. Here:

context.TableCells.Add(tc);

The exception says, “Collection was modified; enumeration operation may not execute.”

Mansur
  • 1,622
  • 14
  • 27
  • The code is not enough to provide any help. Your method **createTableWithCells** creates a new object of Table without any properties being set and then in the same method you are looping through the columns of that table. Not sure how you got that list of columns! Plz follow this guideline to provide enough code for anyone to help you. https://stackoverflow.com/help/mcve – dj079 Oct 31 '18 at 02:19
  • I wrote it with the comment line. "//defining table properties but I don't add any cell object here." Of course this is not the real code because real code is a real mess and there is no need to put everything in here. It might confuse people. That's why i wrote //defining table properties which means I define necessary properties in there but i did not show all of them to you. Sorry to beeing not clear. Let me edit. – Mansur Oct 31 '18 at 11:13
  • Join this chat, we can discuss it there. https://chat.stackexchange.com/rooms/85141/question-discussion – dj079 Oct 31 '18 at 12:02
  • I must have 20 reputation on The Stack Exchange Network to talk there. I am a new user. Sorry. – Mansur Oct 31 '18 at 12:12
  • Did you try that link? I gave you access.. – dj079 Oct 31 '18 at 12:13
  • I did it says that. "You must have 20 reputation on The Stack Exchange Network to talk here." – Mansur Oct 31 '18 at 12:14
  • You are in the room... – dj079 Oct 31 '18 at 12:17
  • Yes I can't send any messages it is blocked. – Mansur Oct 31 '18 at 12:18
  • Ohhh sorry...SO says I could provide you access to send messages and I did, but it seems there are still some additional policies. – dj079 Oct 31 '18 at 12:20

1 Answers1

0

I have solved the problem thanks to @JaredPar 's answer in Collection was modified; enumeration operation may not execute

I have changed the following code:

TableCell cell = new TableCell
{
    //cell elements, properties etc.
    SomeObjects = someObjectList,
    Table = table
};

to

TableCell cell = new TableCell
{
    //cell elements, properties etc.
    SomeObjects = someObjectList.toList(),
    Table = table
};
Mansur
  • 1,622
  • 14
  • 27