つまり、シナリオを一言で言えば、同じエンティティ (階層構造) 内の主キーへの外部キーを持つエンティティ (EF4) があります。
MyEntityId (主キー) ParentMyEntityId (主キーへの外部キー)
両方の EntityState が Unchanged である MyEntityList がある場合List<MyEntity>
:
entity 1 - {MyEntityId = 10, ParentMyEntityId = null}
entity 2 - {MyEntityId = 11, ParentMyEntityId = 10}
そして、私はこれを行います:
//Initially has 2 items in 'in' clause - iterates once and then exits because the EntityState of the second item has changed to Modified
foreach(MyEntity m in MyEntityList.Where(e => e.EntityState == System.Data.EntityState.Unchanged))
{
db.DeleteObject(m);
}
最初の MyEntity は削除されますが、2 番目は "Modified" に変更され、foreach は 2 回目は実行されません。外部キー制約が原因であると推測しています。
しかし、もしそうなら:
//Iterates twice, even though the EntityState of the second item has changed to Modified
foreach(MyEntity m in MyEntityList.Where(e => e.EntityState == System.Data.EntityState.Unchanged).ToList())
{
db.DeleteObject(m);
}
両方のエンティティが削除されます (これは望ましい効果です)。
解決策はありますが、なぜこれが起こるのかに興味があります.foreachループの開始時に定義されたイテレータ「セット」が同じままであるか、実行しようとすると実行時エラーをスローしたという印象を常に受けていました.それを変更します。
Whereを使用しないでください。これを行うより良い方法はありますか?