Jackal
Jackal

Reputation: 3521

Load ICollection from ICollection Entity Framework Core

I have these models which all of them are a one to many relation in succession.

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

Here is what i have tried, however the theninclude does not find the properties

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

this does not work

Upvotes: 1

Views: 385

Answers (1)

Darjan Bogdan
Darjan Bogdan

Reputation: 3910

Using ThenInclude on Many-to-Many relations is supported by Entity Framework Core itself, and compiler should be able to cope with the presented code. However, there is/was a bug in Intellisense and Visual Studio(s), which didn't properly display properties you can use. (in your case Etiquetas). Can confirm, it is fixed in VS 2019 (16.2.0 Preview 1.0) version.

Upvotes: 1

Related Questions