Derived Models - Nested - Prefetches?

Posts   
 
    
usschad
User
Posts: 71
Joined: 11-Sep-2008
# Posted on: 19-Jan-2018 05:33:29   

LLBLGen: 5.3.3 .net 4.6.1 Adapter Template Postgres 9.4

I'm new to Derived Models, but it looks like a great idea. I just need to learn how to use it.

To start, I created a new Derived Model (DTO) with a root entity along with some child objects. I added the fields that I wanted displayed. Then in the Shape Editor, I de-normalized it so the shape is only two levels deep, where the true hierarchy is about 4.

It looks good from the designer. I generated the llblgen code and when I try to use it, I followed the example in the documentation:

List<Customer> results = null;
using(var adapter = new DataAccessAdapter())
{
    var metaData = new LinqMetaData(adapter);
    var q = (from c in metaData.Customer
             where c.VisitingAddressCountry == "USA"
             select c)
            .ProjectToCustomer();
    results = q.ToList();
}

at http://www.llblgen.com/Documentation/5.1/Derived%20Models/dto_llblgen.htm

I used this code exactly, replacing the dto, entity, and filter column names with my own. And what I noticed was that it automatically prefetched one entity (where the FK was on the root node). E.g My root is 'TransactEntity'. It has a 'SellerId'. The 'SellerEntity' data was automatically prefetched. However, there is an 'LcEntity' that has a 'TransactId'. The 'LcEntity' data was not automatically prefetched. Is this normal behavior?

I did a little bit of studying, and I found this regarding prefetches:

var q = (from c in metaData.Customer
         where c.Country == "Germany"
         select c).WithPath(customerPath=>customerPath
              .Prefetch<OrderEntity>(c=>c.Orders)
                     .FilterOn(o=>o.EmployeeId==2)
                     .SubPath(orderPath=>orderPath.Prefetch(o => o.Employee))
                .Prefetch<EmployeeEntity>(c=>c.EmployeeCollectionViaOrder));

at https://www.llblgen.com/Documentation/5.3/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Linq/gencode_linq_prefetchpaths.htm#multiple-nodes-at-the-same-level

So I'm wondering if I need to add the prefetches to the query. But if so, do I not add the prefetch that is happening automatically (i.e. the SellerEntity that is automatically loading)? It's confusing to me, why it chooses to prefetch some entities, but not the others. I suppose it has to do with it prefetching the child if the FK is on the parent... but I didn't pick up on any of this in the documentation.

I can share the llblgen project if that helps, but I'd like to move that to a private channel.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Jan-2018 07:04:04   

The automatic prefetches correspond to entities that you use in your DTO hierarchy (i.e.: you included some fields of that related tables, so they must be prefetched in order to those fields to be accessed).

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 19-Jan-2018 11:42:14   

Indeed, just fetch the root entity, the dto's are prefetched through the navigation in the projection, which are nested queries and act like prefetches. No need for extra data to prefetch or specify.

Frans Bouma | Lead developer LLBLGen Pro
usschad
User
Posts: 71
Joined: 11-Sep-2008
# Posted on: 19-Jan-2018 17:58:45   

But that's what I don't understand. I am adding fields from my LcEntity into my Transact Root Entity in the DTO and it is not automatically fetching the data. Seems like it should, especially from what you are saying. Or am I interpreting this incorrectly?

How can I send my project and code so that it isn't on the public forum? From what you are saying, I think I should expect my fields added onto the DTO should be fetched.

usschad
User
Posts: 71
Joined: 11-Sep-2008
# Posted on: 19-Jan-2018 18:46:39   

I'm under the gun, so I'm trying everything...

I tried calling .WithPath to that linq query, passing it the prefetchPatch that was created like approach 3 from this document: https://www.llblgen.com/Documentation/5.3/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Linq/gencode_linq_prefetchpaths.htm#multiple-nodes-at-the-same-level

It didn't seem to work at all. My related fields in the DTO are still not being loaded.

usschad
User
Posts: 71
Joined: 11-Sep-2008
# Posted on: 19-Jan-2018 19:04:55   

I think you should add to the posting guidelines: "Don't be Stupid."

I entered in the data by hand and forgot to put the FKs in a linking table. #facepalm.

The data is fetching as expected now; without adding the prefetch path.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 20-Jan-2018 09:26:56   

smile Glad it's clear now simple_smile

Frans Bouma | Lead developer LLBLGen Pro