我目前正在使用EF 4.0。我的目標是刪除一個子集合並向同一個父集添加新集合。實體框架集合已被修改;枚舉操作可能不會執行
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? – 2012-02-23 02:02:23
@JustinNiessner:謝謝,我編輯了我的問題。 – madatanic 2012-02-23 02:07:04
現在我也想知道父母的定義。 – 2012-02-23 02:09:58