Prefetch Path Question

Posts   
 
    
Posts: 15
Joined: 13-Mar-2011
# Posted on: 13-Jun-2011 07:57:20   

Hi,

Using 3.1 Adapter gen code, I am trying to populate an object graph from the Adventure Works Database (not 2008 version) with the following code:


        public EntityCollection GetAllCustomers()
        {
            EntityCollection customers = new EntityCollection(new ContactEntityFactory());
            IPrefetchPath2 prefetchPathCustomers = new PrefetchPath2((int)EntityType.ContactEntity);
            IPrefetchPathElement2 preFetch1 = prefetchPathCustomers.Add(ContactEntity.PrefetchPathIndividuals);
            IPrefetchPathElement2 preFetch2 = prefetchPathCustomers.Add(ContactEntity.PrefetchPathIndividuals);
            preFetch1.SubPath.Add(IndividualEntity.PrefetchPathCustomer)
                .SubPath.Add(CustomerEntity.PrefetchPathCustomerAddresses)
                .SubPath.Add(CustomerAddressEntity.PrefetchPathAddress);
            preFetch2.SubPath.Add(IndividualEntity.PrefetchPathCustomer)
                .SubPath.Add(CustomerEntity.PrefetchPathCustomerAddresses)
                .SubPath.Add(CustomerAddressEntity.PrefetchPathAddressType);

            using (DataAccessAdapter adapter = AdaptorFactory.GetNewAdaptor(true))
            {
                adapter.FetchEntityCollection(customers, null, prefetchPathCustomers);
            }

            return customers;
        }

The difficulty is that AddressType is a parent table of CustomerAddress and not Address. So I could not continue the 1st sub-path branch from Address to AddressType.

The code above failed the following error message: The PrefetchPathElement you to tried to add is already added to this PrefetchPath. Parameter name: elementToAdd

Is there a way to achieve what I want to achieve? The idea is to load the Contacts and then a whole bunch of other information about the Contacts e.g. address, address type.

Cheers

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Jun-2011 10:07:54   

First of all the error is because these are duplicates:

IPrefetchPathElement2 preFetch1 = prefetchPathCustomers.Add(ContactEntity.PrefetchPathIndividuals); IPrefetchPathElement2 preFetch2 = prefetchPathCustomers.Add(ContactEntity.PrefetchPathIndividuals);

I think what you need is the following:

IPrefetchPath2 prefetchPathCustomers = new PrefetchPath2((int)EntityType.ContactEntity);

var prefecthElement = prefetchPathCustomers.Add(ContactEntity.PrefetchPathIndividuals).SubPath.Add(IndividualEntity.PrefetchPathCustomer).SubPath.Add(CustomerEntity.PrefetchPathCustomerAddresses);

prefecthElement .SubPath.Add(CustomerAddressEntity.PrefetchPathAddress);
prefecthElement .SubPath.Add(CustomerAddressEntity.PrefetchPathAddressType);
Posts: 15
Joined: 13-Mar-2011
# Posted on: 13-Jun-2011 17:26:19   

Hi Walaa,

That's exactly what I was after! I'm so annoyed I did not figure that out. I was on the right track. I just did not quite understand how to build that graph. But your solution clears that up.

Thanks! Have a great week!