20

This error is thrown a lot, but I can't find the solution. I am new to the Entity Framework and in my first approach I got this error.

This is what I have. I have a company class and a branch class. Both classes have their own repository. A company has one Branch, while one branch can have multiple companies.

In my GUI I fill a combo with Branch objects, which I get from my BranchRepository:

    public IList<Branch> GetAllBranches()
    {
        var query = _context.Branches;

        IList<Branch> branches = query.ToList();

        return branches;
    }

This is result is the datasource of the branch combobox.

When I want to save the company, I do something like this:

company.VisitorAddress = txtVisitAddress.Text;
company.City = txtCity.Text;
company.CompanyName = txtCompany.Text;
company.PhoneNumber = txtPhoneNumber.Text;
company.ZipCode = txtZipcode.Text;
company.Branch = ((Branch)cmbBranches.SelectedItem);
company.Website = txtWebsite.Text;

Then, I call my company repository to save my company. Here is what the save method looks like:

public bool Save(Company company)
{
    _context.AddToCompanies(company);   // <-- This is where the error is thrown.
    _context.SaveChanges();

    return true;
}

When the save method is invoked, I get the error 'An entity object cannot be referenced by multiple instances of IEntityChangeTracker'.

Clearly I'm doing something wrong, but what?

Rap
  • 6,250
  • 2
  • 45
  • 80
Martijn
  • 22,949
  • 59
  • 161
  • 247

1 Answers1

22

Do you create new ObjectContext instance for each your repository? That could be source of problem because when you add Branche to Company it tries to add it to ObjectContext instance which cannot be done because it is still related to ObjectContext instance used to fill combobox. The way to go is to share ObjectContext instance among your repositories. Other possibility is to Detach Branch from first repository but it can have other consequencies.

Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • Yes, each repository has it's own `ObjectContext `. So how can I in a neat way share the `ObjectContext`? Is using a singleton a good way? – Martijn Feb 26 '11 at 17:12
  • No singleton is not a good way - check my answer here: http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392 The way to go can be passing ObjectContext instance to repository's constructor but then you must handle SaveChanges outside of repository. – Ladislav Mrnka Feb 26 '11 at 17:15
  • I'm using winforms. But I don't see how to share the 'ObjectContext'. I don't want the client to be aware of this context. – Martijn Feb 26 '11 at 17:20
  • 2
    You are already using Repository pattern. This pattern is usually used with UnitOfWork pattern. Unit of work wraps the ObjectContext and is able to call Save. All repositories receive UnitOfWork instance as constructor parameter to have access to ObjectContext. – Ladislav Mrnka Feb 26 '11 at 17:37
  • 1
    I don't get it. Can you provide some (dummy) code to explain all this? – Martijn Feb 26 '11 at 17:41
  • @Martijn: I can provide you an example but I will only rewrite something which was answered several times. Check for example here (skip the part with dependency injection if you don't need it): http://stackoverflow.com/questions/3714317/entity-framework-repository-unit-or-work-question You can also simply browse related questions: http://stackoverflow.com/search?q=entity-framework+unit+of+work+repository – Ladislav Mrnka Feb 26 '11 at 19:20
  • See an answer I got in the past: `Basically, I would suggest creating a fresh context for each unit of work` http://stackoverflow.com/questions/14359297/execution-of-the-command-requires-an-open-and-available-connection-the-connecti – Elad Benda Feb 10 '13 at 09:31