Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:20:56+00:00 2026-06-12T16:20:56+00:00

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

  • 0

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;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:20:57+00:00Added an answer on June 12, 2026 at 4:20 pm

    This error is indeed thrown when the entity you’re trying to update is attached to a different objectcontext than the context you’re using to update it. Only one objectcontext can track an entity

    You can detach objects from it’s context by calling:

    where context is the context the entity currently is attached to.

    using (var context = new UnicornsContext())
    {
        var unicorn = new Unicorn { Name = "Franky", PrincessId = 1};
        context.Entry(unicorn).State = EntityState.Detached;
        context.SaveChanges();
    }
    

    http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx

    And in your implementation:

    public void UpdateAddingCandidate(Event eventObj, Candidate candidate)
    {
        db.Entry(candidate).State = EntityState.Detached;
        db.SaveChanges();
    
        eventObj.Candidate = candidate;
        db.Entry(eventObj).State = EntityState.Modified;
        db.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use NHibernate with the Repository-Pattern. The Repositories has the Methods Insert() and Update().
I use the repository pattern and UnitOfWork. This is kept in a separate project
Watching few examples that use repository pattern like StoreFront I couldn't figure out where
I currently use the repository pattern in my ASP.NET MVC 3 app. I use
Is it possible to use a public repository like github and have a branch
I have a FTP server which I use for Linux repository for RPM packages.
I use Assembla to manage my Subversion repository. I have started some major revisions
What Maven repository can I use for EJB 3.1 API dependency?
I'm trying to use git on windows to clone a remote repository. I can
Does Git only use the remote name origin for a repository which was created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.