无法从数据库中读取链接数据

Cannot read linked data from DB

我已经配置了这两个实体:

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; }
}

我手动在数据库中插入了 HostHostLine 的一个元组,并相应地链接它们。

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

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

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

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

HostLineHost table 的 FK 配置如下面的屏幕截图所示。

然后,我实现了一个简单的服务,它扩展 AsyncCrudAppService 以在 HostHostLine 实体上提供 CRUD 操作。

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);
    }

}

我将覆盖添加到方法 MapToEntityDto 以便查看从数据库读取的数据(它将被删除)。 当我通过 Angular 客户端调用服务的 Get REST 方法时,我可以访问 MapToEntityDto() 方法,但是 Host 实体没有 HostLines List 字段。 所以似乎 Host 存储库没有从 HostLine table.

读取链接数据

我是否缺少某种配置来让 Host 存储库也读取 HostLine 数据?

谢谢

您需要使用以下代码手动包含 HostLines。

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);
    }

//...

}

更多信息见https://aspnetboilerplate.com/Pages/Documents/Application-Services#crud-permissions