Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Entity Framework "An entity object cannot be referenced by multiple instances of IEntityChangeTracker"

Tags:

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?

like image 619
Martijn Avatar asked Feb 26 '11 17:02

Martijn


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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.

like image 153
Ladislav Mrnka Avatar answered Sep 20 '22 14:09

Ladislav Mrnka