Getting primary key of intermediate mapping table when using autogenerated EntityCollection properties

Posts   
 
    
Flynn
User
Posts: 17
Joined: 28-Jan-2009
# Posted on: 24-Aug-2009 17:00:10   

I have a two entity tables, Catalog and Course, and a mapping table, CatalogCourseMap, that relates the two entities with a m:n relationship.

LLBLGen has generated a CatalogEntity with Courses property that allows access via the m:n relationship to a CourseEntityCollection. When iterating through the list of returned CourseEntities, I need to access the primary key value of the CatalogCourseMap record that relates the Catalog Entity to the Course Entity.

Currently I am performing a separate database call similar to the following:


IPredicateExpression filter = new PredicateExpression();
filter.Add(CatalogCourseMapFields.CatalogGuid == catalog.CatalogGuid);
filter.AddWithAnd(CatalogCourseMapFields.CourseGuid == course.CourseGuid);
CatalogCourseMapCollection catalogCourseMaps = course.GetMultiCatalogCourseMap(true, filter);

if (catalogCourseMaps.Count > 0)
{
    CatalogCourseMapEntity catalogCourseMap = catalogCourseMaps[0];
    return catalogCourseMap.CatalogCourseMapGuid;
}

Is there a way to access the CatalogCourseMapGuid for the implied relationship without a separate database call?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 24-Aug-2009 17:43:54   

If you want to fetch data from the intermediate table, then don't use the hopped relation (Courses..via...), instead use the relations from main entity to the intermediate and then to the end entity.

In other words, when fetching the CatalogEntity, use a prefetchPath to fetch the intermediate related entities, and then use a subPath (prefetchPath) to fetch the related Courses.