私は現在EF 4.0を使用しています。私の目的は、子コレクションを削除し、新しいコレクションを同じ親に追加することです。Entity Frameworkコレクションが変更されました。列挙操作が実行されない可能性があります
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
}
}
エラーはタイトルと同じです:コレクションが変更されました。列挙操作が実行されないことがあります。
ご協力いただければ幸いです。
newKidsはどこで定義しますか? –
@JustinNiessner:ありがとう、私は私の質問を編集しました。 – madatanic
今私は親がどこに定義されているのだろうか。 –