Fields mapped on relations

Posts   
 
    
Posts: 30
Joined: 08-Apr-2008
# Posted on: 08-Apr-2008 09:32:47   

The firm I work at is considering the use of LLBLGen Pro v2 for a project and I've been asked to take a look at it.

I got started with the basics (CRUD from a single table) and I like what I see. I decided to take it to the next level by trying to use "Fields mapped on relations" but can't seem to get it to work.

This is the scenario that I've setup: 1. A Country table with columns Id and Name. 2. A City table with columns Id, Name and CountryId.

I'm retrieving all the cities from the City table using FetchEntityCollection and then access the Country property for the City object returned, but it's null. I've also tried getting all the countries from the Country table for which the City property for the Country object returns an EntityCollection (typed City via generics) that contains 0 elements.

Am I missing a method call somewhere?

Any help appreciated.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Apr-2008 09:51:01   

Most probably you are using the Adapter model, which doesn't have LazyLoading for the related fields.

So all you have to do is to pass a PrefetchPath to the Fetch method. Please check the manual's section "Using the generated code -> Adapter -> Prefetch paths"

Posts: 30
Joined: 08-Apr-2008
# Posted on: 08-Apr-2008 12:13:35   

Hi!

It worked with the addition of the prefetch path. Thanks for your help!


DataAccessAdapter daa = new DataAccessAdapter();
EntityCollection<DataAccess.EntityClasses.CountryEntity> countryColl = new EntityCollection<DataAccess.EntityClasses.CountryEntity>();

//following 2 lines added for prefetch
PrefetchPath2 prefetchpath = new PrefetchPath2((int) EntityType.CountryEntity);
prefetchpath.Add(CountryEntity.PrefetchPathCity);

daa.FetchEntityCollection(countryColl, null, prefetchpath);

foreach (DataAccess.EntityClasses.CountryEntity iterCountry in countryColl)
{
    int cityCount = iterCountry.City.Count;
    Console.WriteLine(iterCountry.Name + " has " + cityCount.ToString() + " cities.");
}

Regards, NiTiN