Organizational Research By

Surprising Reserch Topic

how to de attach an entity from a context in entity framework


how to de attach an entity from a context in entity framework  using -'asp.net,entity-framework,entity-framework-4,repository-pattern'

I use EF 4.1 with Repository and DbContext.. POCO with T4 template.
For every Repository I use a separate DbContext.

I need to update an object with has a related property, at the moment I receive this error

An entity object cannot be referenced by multiple instances of IEntityChangeTracker.


I suppose my problem is beacuse eventObj and candidate are created from different Repositories.

So i'm trying to solve the problem with this code, with no success.

My question?


How do I get rid of this error?
Is is possible remove the candidate from its context?

public void UpdateAddingCandidate(Event eventObj, Candidate candidate){
    Event updatedEvent = new Event();
    Candidate updatedCandidate = new Candidate();
    updatedEvent = eventObj;
    updatedCandidate = candidate;
    updatedEvent.Candidate = updatedCandidate;
    db.Entry(updatedEvent).State = EntityState.Modified;     
}



EDIT

    public void UpdateAddingCandidate(Event eventObj, Candidate candidate)
    {
        /*
        db.Events.AsNoTracking();
        db.Candidates.AsNoTracking();
        */
        db.Entry(eventObj).State = EntityState.Detached;
        db.Entry(candidate).State = EntityState.Detached;

        Event updatedEvent = new Event();
        Candidate updatedCandidate = new Candidate();
        updatedEvent = eventObj;
        updatedCandidate = candidate;
        updatedEvent.Candidate = updatedCandidate;
        db.Entry(updatedEvent).State = EntityState.Detached;
        db.Entry(updatedEvent).State = EntityState.Modified;

    }

    
asked Sep 22, 2015 by akhilesh
0 votes
21 views



Related Hot Questions



Government Jobs Opening


...