从 ICollection Entity Framework 核心加载 ICollection
Load ICollection from ICollection Entity Framework Core
我有这些模型,它们都是连续的一对多关系。
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();
这行不通
在 Many-to-Many 关系上使用 ThenInclude
由 Entity Framework 核心本身支持,编译器应该能够处理提供的代码。但是,在 Intellisense 和 Visual Studio(s) 中有 is/was a bug,它们没有正确显示您可以使用的属性。 (在你的情况下 Etiquetas
)。可以确认,在VS 2019 (16.2.0 Preview 1.0) 版本中修复。
我有这些模型,它们都是连续的一对多关系。
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();
这行不通
在 Many-to-Many 关系上使用 ThenInclude
由 Entity Framework 核心本身支持,编译器应该能够处理提供的代码。但是,在 Intellisense 和 Visual Studio(s) 中有 is/was a bug,它们没有正确显示您可以使用的属性。 (在你的情况下 Etiquetas
)。可以确认,在VS 2019 (16.2.0 Preview 1.0) 版本中修复。