Null parent prefetch problem

Posts   
 
    
jedrzejm
User
Posts: 3
Joined: 10-Mar-2011
# Posted on: 10-Mar-2011 14:40:28   

Hi, I've just started using LLBLGen so maybe it's stupid problem but I'm completely stuck with it... I have situation like this: EmployeeEntity has collection of EmployeeCar entities, and reference to Manager (also EmployeeEntity), and also EmployeeCarEntity has refference to the Employee

I want to choose all cars and manager. This is my test code:

EntityCollection<EmployeeCarEntity>  GetCarsForEmployee(int employeeId)
{
    EmployeeEntity employee = new EmployeeEntityEntity(employeeId);
    DataAccessAdapter adapter = CreateAdapter();

    EntityCollection<EmployeeCarEntity> employeeCars = employee.EmployeeCars;
    
    /******/
    IPrefetchPath2 employeePrefetchPath = GetPath();
    /******/
    
    adapter.FetchEntityCollection(employeeCars, employee.GetRelationInfoEmployeeCars(), employeePrefetchPath)
    
    adapter.CloseConnection();
    
    return employeeCars;
}

IPrefetchPath2 GetPath()
{
IPrefetchPath2 employeePrefetchPath = new PrefetchPath2((int)EntityType.EmployeeCarEntity);
(*) employeePrefetchPath.Add(EmployeeCarEntity.PrefetchPathEmployeeUser;


return employeePrefetchPath;
}

Everything works but .... When Manager reference in database is NULL (for example employee is a company owner) it stops working and returns empty collection. The problem is in line with (*), because without this lines functions works fine. I know that I am not using

Maybe someone had problem like this?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Mar-2011 20:39:11   

jedrzejm wrote:

I have situation like this: EmployeeEntity has collection of EmployeeCar entities, and reference to Manager (also EmployeeEntity), and also EmployeeCarEntity has refference to the Employee

Sorry, I don't understand your hierarchy. Could you please explain how the elements are related and who is parent of who.

David Elizondo | LLBLGen Support Team
jedrzejm
User
Posts: 3
Joined: 10-Mar-2011
# Posted on: 11-Mar-2011 10:08:58   

I have created test project (in attachment) so I hope it will help. In attachment there are also sql scripts with test database, and test data.

I investigated the problem more, and I found that the problem is not with the Null manager.

Please read the code below:

        EmployeeEntity employeeEntity = new EmployeeEntity(3);
        EntityCollection<CarEntity> cars = employeeEntity.Cars;
        IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.CarEntity);

/* problem line */ prefetchPath.Add(CarEntity.PrefetchPathEmployee); adapter.FetchEntityCollection(cars, employeeEntity.GetRelationInfoCars(), prefetchPath);

        /***/
        /***   cars.Count = 3; */
        /***/

        EmployeeEntity employeeEntityChild = new EmployeeEntity(3);
        EntityCollection<CarEntity> cars2 = employeeEntityChild.Cars;
        IPrefetchPath2 prefetchPath2 = new PrefetchPath2((int)EntityType.CarEntity);

/* problem line */ //prefetchPath.Add(CarEntity.PrefetchPathEmployee);

        adapter.FetchEntityCollection(cars2, employeeEntityChild.GetRelationInfoCars(), prefetchPath2);

        /***/
        /***   cars2.Count = 6; */
        /***/

Same of the records are lost when I add: prefetchPath.Add(CarEntity.PrefetchPathEmployee);

Could you tell why?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Mar-2011 10:37:23   

Reproduced using: LLBLGen Pro v.3.1 .NET 4.0 / VS 2010 SQL Server Express 2008 R2.

Looking into it.

(Edit) The following returns 6 cars (correct results)

            EntityCollection<CarEntity> cars3 = new EntityCollection<CarEntity>();
            IPrefetchPath2 prefetchPath3 = new PrefetchPath2((int)EntityType.CarEntity);
            prefetchPath3.Add(CarEntity.PrefetchPathEmployee);
            adapter.FetchEntityCollection(cars3, null, prefetchPath3);

Could you try it please.

jedrzejm
User
Posts: 3
Joined: 10-Mar-2011
# Posted on: 11-Mar-2011 11:45:21   

Thanks for fast answer.

Your case works, but I want to achieve something else.

I want to select all cars for employee with id 3 (your code returns all cars), and each car should have his owner employee entity filled (your code works fine here)

Also could you explain why my code in first case is not working?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 11-Mar-2011 13:41:00   

We'll look into it.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 11-Mar-2011 18:06:02   

I'm not sure what you're trying to do, as it seems you're fetching a collection of EmployeeCarEntity which is part of an Employee entity, however that employeeEntity isn't returned.

So IMHO you should create an EntityCollection<EmployeeCarEntity>() instance and a filter, using: EmployeeCarFields.EmployeeId==3

and fetch that as the collection. Then the path has to be: IPrefetchPath2 employeePrefetchPath = new PrefetchPath2((int)EntityType.EmployeeCarEntity); employeePrefetchPath.Add(EmployeeCarEntity.PrefetchPathEmployeeUser);

However this doesn't fetch manager in any way, you're fetching EmployeeCar entities, not employees.

So I am lost in what you actually want to achieve, as what you showed doesn't (IMHO) match with what you described, so I don't know what to suggest to you as the code which does what you want.

Also, could you enable DQE tracing (see 'troubleshooting and debugging' to see which queries are executed?

Frans Bouma | Lead developer LLBLGen Pro