1
我有,有时有自我参照实体自我引用的表,如下所示: (是的,IdFamily为空)实体框架和自我引用表和自我参照实体
...
//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();
很遗憾听到它,但感谢澄清。我无法找到任何有关它的信息,只是自引用不同的实体表。我很乐意为EF做出贡献,我也计划实施DbContext.IsDirty,我只是不知道我是否具备这些技能,EF代码库是巨大的。 – 2013-02-25 18:42:31