实体对象不能被 IEntityChangeTracker 的多个实例引用.在 Entity Framework 4.1 中向实体添加相关对象时 [英] entity object cannot be referenced by multiple instances of IEntityChangeTracker. while adding related objects to entity in Entity Framework 4.1

查看:29
本文介绍了实体对象不能被 IEntityChangeTracker 的多个实例引用.在 Entity Framework 4.1 中向实体添加相关对象时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存 Employee 详细信息,其中包含 City 的引用.但是每次我尝试保存已验证的联系人时,我都会收到异常 ADO.Net 实体框架实体对象不能被 IEntityChangeTracker 的多个实例引用"

I am trying to save Employee details, which has references with City. But everytime I try to save my contact, which is validated I get the exception "ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker"

我已经阅读了很多帖子,但仍然不知道该怎么做...我的保存按钮点击代码如下

I had read so many post but still not getting the exact idea of what to do... my Save button click code is given 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);
    }

员工服务代码

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";
        }

推荐答案

因为这两行...

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

... 不要在构造函数中使用参数,我猜您在类中创建了一个上下文.当你加载 city1...

... don't take a parameter in the constructor, I guess that you create a context within the classes. When you load the city1...

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

...您将 city1 附加到 CityService 中的上下文.稍后您添加一个 city1 作为对新 Employee e1 的引用,并添加 e1 包括此引用到 city1EmployeeService 中的上下文.因此,您将 city1 附加到两个不同的上下文,这就是异常所抱怨的.

...you attach the city1 to the context in CityService. Later you add a city1 as a reference to the new Employee e1 and add e1 including this reference to city1 to the context in EmployeeService. As a result you have city1 attached to two different context which is what the exception complains about.

您可以通过在服务类之外创建上下文并在两个服务中注入和使用它来解决此问题:

You can fix this by creating a context outside of the service classes and injecting and using it in both services:

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

您的服务类看起来有点像只负责单一实体类型的存储库.在这种情况下,当您为服务使用单独的上下文时,一旦涉及实体之间的关系,您就会总是遇到麻烦.

Your service classes look a bit like repositories which are responsible for only a single entity type. In such a case you will always have trouble as soon as relationships between entities are involved when you use separate contexts for the services.

您还可以创建负责一组密切相关实体的单一服务,例如 EmployeeCityService(具有单一上下文)并在您的 Button1_Click<中委托整个操作/code> 方法到此服务的方法.

You can also create a single service which is responsible for a set of closely related entities, like an EmployeeCityService (which has a single context) and delegate the whole operation in your Button1_Click method to a method of this service.

这篇关于实体对象不能被 IEntityChangeTracker 的多个实例引用.在 Entity Framework 4.1 中向实体添加相关对象时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆