从ICollection实体框架核心加载ICollection

时间:2019-05-27 10:51:07

标签: c# asp.net-core-2.2 entity-framework-core-2.2

我拥有这些模型,它们都是连续的一对多关系。

ListaAbastecimento> ReferenciaAbastecimento> EtiquetaAbastecimento

[Table(name: "hListasAbastecimento")]
public class ListaAbastecimento
{
    public int Id { get; set; }
    public int ColaboradorId { get; set; }   
    [ForeignKey("ColaboradorId")]
    public virtual Colaborador Colaborador { get; set; }

    public string UAP { get; set; }
    public DateTime DataCriacao { get; set; }

    public virtual ICollection<ReferenciaAbastecimento> Referencias { get; set; }
}




    [Table(name: "hReferenciasAbastecimento")]
    public class ReferenciaAbastecimento
    {
        public int Id { get; set; }

        [MaxLength(15)]
        public string Referencia { get; set; }
        public int? QtdAbastecimento { get; set; }
        public int? QtdCaixas { get; set; }
        public int? QtdPecasPorCaixa { get; set; }

        public virtual ICollection<EtiquetaAbastecimento> Etiquetas { get; set; }
    }




[Table(name: "hEtiquetasAbastecimento")]
    public class EtiquetaAbastecimento
    {
        public int Id { get; set; }
        public int? EtiquetaFIFO { get; set; }
        public int? Qtd { get; set; }

        [MaxLength(20)]
        public string Localizacao { get; set; }

        public int ReferenciaAbstecimentoId { get; set; }
        [ForeignKey("ReferenciaAbstecimentoId")]
        public virtual ReferenciaAbastecimento ReferenciaAbastecimento { get; set; }
    }

这是我尝试过的方法,但是theninclude找不到属性

   var abastecimentosList = await _context.ListasAbastecimento
            .Include(la => la.Referencias)
            .ThenInclude(r => r.Etiquetas) // can't find Etiquetas property
            .ToListAsync();

这不起作用

1 个答案:

答案 0 :(得分:1)

实体框架核心本身支持在多对多关系上使用ThenInclude,并且编译器应该能够处理所提供的代码。但是,Intellisense和Visual Studio中有一个bug,但没有正确显示您可以使用的属性。 (在您的情况下为Etiquetas)。可以确认,它已在VS 2019(16.2.0 Preview 1.0)版本中修复。