简体   繁体   中英

Does EntitiesToDTOs (Entity Framework DTO Generator) support Lazy Loading?

I've got a couple of entities in a parent-child relationship: Family (parent) and Updates (child). I want to read a list of families without the corresponding updates. There are only 17 families but about 60,000 updates so I really don't want the updates.

I used EntitiesToDTOs to generate a DTO from the Family entity and to create an assembler for converting the Family entity to the FamilyDTO. The ToDTO method of the assembler looks like:

public static FamilyDTO ToDTO(this Family entity)
{
    if (entity == null) return null;

    var dto = new FamilyDTO();

    dto.FamilyCode = entity.FamilyCode;
    dto.FamilyName = entity.FamilyName;
    dto.CreateDatetime = entity.CreateDatetime;
    dto.Updates_ID = entity.Updates.Select(p => p.ID).ToList();

    entity.OnDTO(dto);

    return dto;
}

When I run the assembler I find each resulting FamilyDTO has the Updates_ID list populated, although lazy loading is set to true for the EF model (edmx file). Is it possible to configure EntitiesToDTOs to support lazy loading of child elements or will it always use eager loading? I can't see any option in EntitiesToDTOs that could be set to support lazy loading when generating the assembler.

By the way, I'm part of a larger team that uses EntitiesToDTOs to regenerate the assemblers on an almost daily basis, so I'd prefer not to modify the assembler by hand if possible.

I'm Fabian, creator of EntitiesToDTOs .

First of all, thanks a lot for using it .

What you have detected is in fact what I don't want the Assembler to do, I want the developer to map the navigation properties only if needed using the partial methods OnDTO and OnEntity . Otherwise you run into problems like you have.

Seems like I never run into that problem using the tool, THANKS A LOT.

So right now I'm fixing this. It's now fixed in version 3.1 .

Based on the code that you've posted here, and based on how I think someone would implement such a solution (ie to convert records to a DTO format) I think that you would have no choice but to do eager loading.

Some key points:

1) Your Updates_ID field is clearly a List, which means that it's hydrating the collection right away (ToList always executes. Only a regular IEnumerable employs deferred execution).

2) If you're sticking any sort of navigation property in a DTO it would automatically be loaded eagerly. That's because once you so much as touch a navigation property that was brought back by Entity Framework, the framework automatically loads it from the database, and doesn't care that all you wanted was to populate a DTO with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM