Search code examples
c#.netaspnetboilerplate

Cannot read linked data from DB


I have configured this two entities:

Host.cs

public class Host : FullAuditedEntity<int>
{
    [Required]
    public string Name { get; set; }

    public List<HostLine> HostLines { get; set; }
}

HostLine.cs

public class HostLine: FullAuditedEntity<int>
{
    [Required]
    public string Name { get; set; }

    public Host Host { get; set; }
}

Manually, I inserted in the DB one tuple for Host and HostLine, linking them accordingly.

select Id, Name from [MyDB].dbo.Hosts;

Id | Name
-----------
1  | Host1

select Name, HostId from [MyDB].dbo.HostLines;

HostId | Name
------------------
1      | HostLine1

The HostLine's FK to the Host table is configured as you can see in the screenshot below.

enter image description here

Then, I implemented a simple Service which extends AsyncCrudAppService to provide CRUD operation on both the Host and HostLine entities.

public class HostsAppService : AsyncCrudAppService<Host, HostDto, int, PagedResultRequestDto, CreateHostDto, HostDto>, IHostsAppService
{

    public HostsAppService(IRepository<Host> repository)
       : base(repository)
    {
    }

    protected override HostDto MapToEntityDto(Host entity)
    {
        return base.MapToEntityDto(entity);
    }

}

I added the override to the method MapToEntityDto in order to see the data read from the DB (it will be removed). When I call the Get REST method of the service via the Angular client I can reach the MapToEntityDto() method, but the Host entity does not have a value for the HostLines List field. So it seems that the Host repository is not reading the linked data from the HostLine table.

Am I lacking some kind of configuration to let the Host repository read also the HostLine data?

Thank you


Solution

  • You need to include the HostLines manually with the below code.

    public class TaskAppService : AsyncCrudAppService<Host, HostDto, int, PagedResultRequestDto, CreateHostDto, HostDto>, IHostsAppService
    {
    
    //...
        protected override IQueryable<Task> CreateFilteredQuery(GetAllTasksInput input)
        {
            return base.CreateFilteredQuery(input).Include(x=>x.HostLines);
        }
    
    //...
    
    }
    

    Further information see https://aspnetboilerplate.com/Pages/Documents/Application-Services#crud-permissions