I'm currently using EF 4.0. My objective is to delete a child collection and add new ones to same parent.
public void AddKids(int parentId, Kids newKids)
{
using (ModelContainer context = new ModelContainer(connectionString))
{
using (TransactionScope scope = new TransactionScope())
{
var query = from Parent _parent in context.Parents
where _parent.ParentId == parentId select _parent;
Parent parent = query.Single();
while (parent.Kids.Any())
{
context.Kids.DeleteObject(parent.Kids.First());
}
if (newKids != null)
{
foreach (Kid _kid in newKids)
{
parent.Kids.Add(new Kid
{
Age = _kid.Age,
Height = _kid.Height
});
}
}
scope.Complete();
}
context.SaveChanges(); //Error happens here
}
}
The error is as from the title: Collection was modified; enumeration operation may not execute.
Any help would be appreciated.