次のような自己参照エンティティを持つ自己参照テーブルがあります:( はい、IdFamilyはnull可能です)
...
//At my Business
newFile.Family = newFile;
...
//At my ASP.NET MVC action I call:
context.SaveChanges();
DbUpdateExceptionをスローします。
[UpdateException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.]
System.Data.Mapping.Update.Internal.UpdateTranslator.DependencyOrderingError(IEnumerable`1 remainder) +4302030
System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +4300384
System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) +583
System.Data.Entity.Internal.InternalContext.SaveChanges() +382
[DbUpdateException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.]
System.Data.Entity.Internal.InternalContext.SaveChanges() +485
System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +63
System.Data.Entity.DbContext.SaveChanges() +75
SaveChangesを呼び出し、IDを設定して、もう一度SaveChangesを呼び出す必要があるため、回避策は醜いです。UnitOfWorkを壊しているので、回避策を回避するには、すべてのリクエストをTransactionScope内に配置する必要があります。
...
//At my Business
//newFile.Family = newFile;
context.SaveChanges();//BREAK UNIT OF WORK
newFile.IdFamily = newFile.Id;
...
//At my ASP.NET MVC action I call(Action Wrapped in TransactionScope):
context.SaveChanges();