gpt4 book ai didi

c# - Entity Framework 中 "The RelatedEnd cannot be returned by this RelationshipManager"异常的根本原因是什么?

转载 作者:太空狗 更新时间:2023-10-29 23:52:07 24 4
gpt4 key购买 nike

我正在使用 ASP .NET MVC 3.0 Web 应用程序中的 Entity Framework 4.3.1,我有一些代码可以分离旧对象并将其替换为如下所示的新对象:

public void UpdateUnattached(T entryToUpdate, T updatedEntry)
{
var ctx = (EntityFrameworkContext) _context;
ctx.ChangeObjectState(EntityState.Detached, entryToUpdate);
_set.Attach(updatedEntry);
ctx.ChangeObjectState(EntityState.Modified, updatedEntry);
}

这一直有效,除了一种情况,当我在 _set.Attach 行收到下面的异常时。

The RelatedEnd cannot be returned by this RelationshipManager. A RelatedEnd can only be returned by a RelationshipManager for objects that are either tracked by the ObjectStateManager or that implement IEntityWithRelationships. 

对象图非常深,我认为它告诉我有一个属性已设置为一个对象,该对象尚未在某处进行更改跟踪(如果我错了请纠正我)。所以,我的问题是……

我如何找出此异常的根本原因是什么?

我试过在它周围放置一个 try/catch 并在 catch 中调用 GetValidationErrors 但它也会抛出相同的异常。

来自异常的更多信息(InnerException 为 null)...

Type: System.InvalidOperationException 
Source: System.Data.Entity
Target site: GetRelatedEndInternal
Stacktrace: at System.Data.Objects.DataClasses.RelationshipManager.GetRelatedEndInternal(String relationshipName, String targetRoleName)
at System.Data.Objects.EntityEntry.FindRelatedEntityKeysByForeignKeys(Dictionary`2& relatedEntities, Boolean useOriginalValues)
at System.Data.Objects.EntityEntry.TakeSnapshotOfForeignKeys()
at System.Data.Objects.ObjectStateManager.PromoteKeyEntryInitialization(EntityEntry keyEntry, IEntityWrapper wrappedEntity, IExtendedDataRecord shadowValues, Boolean replacingEntry)
at System.Data.Objects.ObjectContext.AttachSingleObject(IEntityWrapper wrappedEntity, EntitySet entitySet, String argumentName)
at System.Data.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
at System.Data.Entity.DbSet`1.Attach(TEntity entity)
at ... snip ...

最佳答案

通过查看错误消息,我猜测您正在尝试附加与 ObjectStateManager 未跟踪的其他对象(即可能不是实体)有关系的对象。

关于c# - Entity Framework 中 "The RelatedEnd cannot be returned by this RelationshipManager"异常的根本原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12560081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com