2012-10-13 2 views
3

디버깅 할 때 ObjectStateManager의 내용을 볼 수 있습니까?

EF에서 엔티티를 업데이트하는 데 문제가 있습니다. 이 오류가 계속 ". 동일한 키를 가진 개체가 이미 ObjectStateManager에 존재하는 ObjectStateManager는 동일한 키를 가진 여러 객체 추적 할 수 없습니다"

를 분명히 다른 기업이 있다는 것을 나는 충분히 알고

을 첨부 된 어딘가에. 그러나, 나는 현재 그것을 추적 할 수 없다. 코드가 많아서 이미 많은 시간을 보냈습니다. 멀리 볼 수있는 한, 내 모든 쿼리에 AsNoTracking() 확장 방법을 사용하고 있습니다. 내가 어떤 주어진 시간에 ObjectStateManager 실제로 무엇인지 알 수있는 방법이 : 나는

내 질문을 필요한

이 무엇입니까? 디버깅하는 동안 해당 항목을 볼 수 있다면 더 빨리이 위치를 추적 할 수 있습니다.

위와 같이 할 수없는 경우이 문제를 해결하는 최선의 방법에 대해 조언 해 주시면 감사하겠습니다. 바로 지금 건초 더미에있는 바늘과 같습니다.

답변

1

이 질문은 도움 :

what is the most reasonable way to find out if entity is attached to dbContext or not?

나는이 방법으로 그것을 구현 :

var attachedEntity = context.ChangeTracker.Entries<T>().FirstOrDefault(x => x.Entity.Id == entity.Id); 

       // If the entity is already attached. 
       if (attachedEntity != null) 
       { 
        // Set new values 
        attachedEntity.CurrentValues.SetValues(entity); 
       } 
       else 
       { 
        // Else attach the entity (if needed) 
        if (context.Entry(entity).State == EntityState.Detached) 
        { 
         Entities.Attach(entity); 
        } 
        // Set the entity's state to modified 
        context.Entry(entity).State = EntityState.Modified; 
       } 
       context.SaveChanges(); 

참고 : Entities 그냥 IDbSet<T>context.Set<T>()에서 위의 코드는 업데이트에서이다() 메소드를 제네릭 저장소에 저장합니다.

0

이 기사 : http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/08/27/9656.aspx에는 ObjectStateManager의 내용을 덤프하는 코드가 포함되어 있습니다. here에서

: 내 경우에는 내가 할 수와 같은 단일 using(Context context = new MyEntities()) 블록 내에서 많은 활동을 내가 할 수있는 한을하려고 있도록

Almost certainly what is happening here is that the object you are trying to delete is not attached, but a related entity or collection loaded on the detached instance is already attached to the ObjectContext. So when you try to attach the entity you are trying to delete, we try to attach the entire graph, which includes entities that are already attached.

문제의이 종류는, 고통을 조금이다.

다른 조언은 개체를 추가하기 전에 개체가 존재하지 않는지 확인하는 것입니다. 일종의 명백한, 나는 알고있다. 그러나 나는 과거의 그 약간 시간에 의해 저것에 의해 위태롭게되었다.

+0

감사합니다. 정보를 감사하지만 내 문제를 어떻게 해결했는지 직접 확인하십시오. – Matt

1

Is there any way I can see what is actually in the ObjectStateManager at any given time? If I can see the items in there during debugging, I can more quickly track down where this is coming from.

또한 Visual Studio 디버거의 빠른 시계 기능을 사용하여 ObjectStateManager에서 무엇을 볼 수 있습니다. 경로는 다음과 같습니다.

context -> ObjectContext -> ObjectStateManager -> Non-Public members

0

가끔 간과되는 : Context.Set<T>.Local() 해당 유형에 대해서만 연결된 엔터티를 제공합니다.

관련 문제