Base solution for your next web application
Open Closed

Dto with related data #3824


User avatar
0
klawsuc created

I want to return a Dto that has data from nested related tables (not sure how else to word this). Here is a sample of what the structure would look like:

  • Household (only one)
  • People (one or more that belong to a house)
  • Pets (one or more that belong to a person...no relation to house)
  • Toys (one ore more that belong to a pet...no relation to house or person)

I would like to return a dto with all the people, pets, and toys at a specific house. Household table has:

public virtual ICollection<Person> People { get; set; }

Person table has:

public virtual ICollection<Pet> Pets { get; set; }

Pet table has:

public virtual ICollection<Toy> Toys { get; set; }

GetHouseDataDto (the dto I am trying to return) has:

public HouseViewDto House { get; set; }
public ICollection<PersonViewDto> People { get; set; }
public ICollection<PetViewDto> Pets { get; set; }
public ICollection<ToyViewDto> Toys { get; set; }

I have the following query:

var house = await _houseRepository
    .GetAll()
    .Include(c => c.People)
    .FirstOrDefaultAsync(f => f.Id == input.Id.Value);
output.House = ObjectMapper.Map<HouseViewDto>(house);
output.People = ObjectMapper.Map< List<PersonViewDto>>(house.People);

How do I also include pets and toys?

Thanks.


3 Answer(s)
  • User Avatar
    0
    klawsuc created

    In case anyone else runs into this issue: <a class="postlink" href="https://github.com/aspnet/EntityFrameworkCore/issues/6560#issuecomment-248749588">https://github.com/aspnet/EntityFramewo ... -248749588</a> I was trying to use ThenInclude but wasn't getting the intellisense.

    However I am still faced with the issue of how to get the nested data into their appropriate dtos. Or am I left with one of the following: for nested data, they can't be in their own dto but rather lazy loaded into the parent dto...in this case PersonViewDto would have a collection of Pet entities which in turn has lazy loaded Toy entities. I would end up with all the columns for pets and toys. run separate queries to get the data into four different dtos.

    Any other options?

  • User Avatar
    0
    klawsuc created

    Another update. Have an icollection of entities does not work with the dto. Not sure why but I get the error "net::ERR_CONNECTION_RESET". As well as a debug message in browser console (assuming from angular) that "Content-Type is not sent!". Sometimes I can open the api link and get the data...other times I get redirected to <a class="postlink" href="http://localhost:22742/Account/Login?ReturnUrl=%2Fapi%2Fservices%2Fapp%2FForm%2FGetFormShow%3FId%3D1">http://localhost:22742/Account/Login?Re ... w%3FId%3D1</a>

    Maybe also worth mentioning that I am using the Core + Angular 4.1.1 version.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @klawsuc,

    ThenInclude is defined in EF Core, if you create a custom repository in EntityFrameworkCore project, you can use it to get your nested data. If you don't want to use it, you can use EnsurePropertyLoadedAsync extension method.

    Can you check your logs.txt file for detailed error message for your last problem ?

    Thanks.