无法为Collection分配新值

时间:2014-07-24 00:06:31

标签: c# asp.net linq entity-framework

我有一个非常奇怪的问题,对我没有任何意义。我正在尝试将一个集合(col)分配给一个属性(PropertyEmployees),但它实际上并没有分配。 该物业最初表示" employee.PropertyEmployees =' employee.PropertyEmployees'抛出了System&ObjectDisposedException'"类型的异常,但即使在我尝试在其上分配新集合之后,它仍然保持这种状态。

col集合在尝试使用之前包含有效数据。

Account和Person属性具有相同的异常,但在为其分配新对象后它会消失。

 public IList<Employee> getEmployees(int landlordId, int propertyId)
    {
        try
        {
            IList<Employee> employees = new List<Employee>();
            IList<PropertyEmployee> propertyEmployees;
            if (propertyId != 0)
            {
                propertyEmployees = propertyEmployeeService.getPropertyEmployeesByPropertyId(propertyId);
                foreach (PropertyEmployee propertyEmployee in propertyEmployees)
                {
                    employees.Add(employeeService.getEmployeeByEmployeeId(propertyEmployee.employeeId));
                }
            }
            else
            {
                employees = employeeService.getEmployeesByLandlordId(landlordId);
            }

            foreach (Employee employee in employees)
            {
                Collection<PropertyEmployee> col = new Collection<PropertyEmployee>(propertyEmployeeService.getPropertyEmployeesByEmployeeId(employee.employeeId));
                employee.PropertyEmployees = col;
                employee.Person = personService.getPersonByPersonID(employee.personId);
                employee.Account = accountService.getAccountByAccountId(employee.accountId.GetValueOrDefault());        
            }

            return employees;
        }
        catch (ErrorHandler e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new ErrorHandler(e.Message);
        }
    }

1 个答案:

答案 0 :(得分:0)

问题在于employee对象的状态(或属性设置器访问的其他对象),与col集合或其元素的值无关。

对已处置对象执行操作时抛出

System.ObjectDisposedException。希望Employee类包含IsDisposed属性,您可以在调用任何employee成员之前检查该属性,

相关问题