Multiple instances of IEntityChangeTracker cannot refer to the same entity object. In Entity Framework 4.1, when adding linked objects to an entity,

ado.net asp.net c# entity-framework foreign-keys

Question

I'm attempting to store employee information that includes city references. But each time I attempt to save my verified contact, I get the exception "Entity Framework for ADO.Net IEntityChangeTracker instances cannot reference the same entity object more than once.".

Despite reading several posts, I was still unsure exactly what to do. My Save button click code is shown below.

protected void Button1_Click(object sender, EventArgs e)
    {
        EmployeeService es = new EmployeeService();
        CityService cs = new CityService();

        DateTime dt = new DateTime(2008, 12, 12);
        Payroll.Entities.Employee e1 = new Payroll.Entities.Employee();

        Payroll.Entities.City city1 = cs.SelectCity(Convert.ToInt64(cmbCity.SelectedItem.Value));

        e1.Name = "Archana";
        e1.Title = "aaaa";
        e1.BirthDate = dt;
        e1.Gender = "F";
        e1.HireDate = dt;
        e1.MaritalStatus = "M";
        e1.City = city1;        

        es.AddEmpoyee(e1,city1);
    }

and 18 to zz

public string AddEmpoyee(Payroll.Entities.Employee e1, Payroll.Entities.City c1)
        {
            Payroll_DAO1 payrollDAO = new Payroll_DAO1();
            payrollDAO.AddToEmployee(e1);  //Here I am getting Error..
            payrollDAO.SaveChanges();
            return "SUCCESS";
        }
1
163
7/9/2012 5:04:04 PM

Accepted Answer

Considering these two lines...

EmployeeService es = new EmployeeService();
CityService cs = new CityService();

I suppose you build a context inside the classes if you don't accept an argument in the constructor. When you assemble thecity1 ...

Payroll.Entities.City city1 = cs.SelectCity(...);

You fasten thecity1 to the setting inCityService afterwards adding acity1 as an allusion to the newEmployeee1 plus adde1 according to the circumstances inEmployeeService Consequently, you havecity1 The exception complains about the contexts being associated to two separate objects.

By establishing a context outside of the service classes, injecting it, and utilizing it in both services, you can remedy this:

EmployeeService es = new EmployeeService(context);
CityService cs = new CityService(context); // same context instance

Your service classes resemble repositories that are exclusively in charge of one entity type. If you employ different contexts for the services in such a situation, you will always run into problems if relationships between entities are involved.

Additionally, you may design a single service to manage a number of entities that are connected in some way, such as anEmployeeCityService (which just has one context) and hand over the whole procedure to yourButton1_Click method to one of this service's methods.

239
4/17/2012 3:35:29 PM

Popular Answer

Reproduction procedures may be expressed as follows:

var contextOne = new EntityContext();
var contextTwo = new EntityContext();

var user = contextOne.Users.FirstOrDefault();

var group = new Group();
group.User = user;

contextTwo.Groups.Add(group);
contextTwo.SaveChanges();

Code that is error-free

var context = new EntityContext();

var user = context.Users.FirstOrDefault();

var group = new Group();
group.User = user; // Be careful when you set entity properties. 
// Be sure that all objects came from the same context

context.Groups.Add(group);
context.SaveChanges();

With only oneEntityContext can handle this." For further solutions, see the other responses.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow