C# 查询存储库时填充相关实体
在我的存储库中,我从TransportedMaterial表中获取数据。我还“包括”TransportedMaterialPacking以检索此链接表中的所有相关数据。在最后一个表中,我还想检索相关的MaterialPacking数据。我不知道怎么做 在上图中,我们看到材料包装为空,但尚未填充 以下是模型:C# 查询存储库时填充相关实体,c#,entity-framework,C#,Entity Framework,在我的存储库中,我从TransportedMaterial表中获取数据。我还“包括”TransportedMaterialPacking以检索此链接表中的所有相关数据。在最后一个表中,我还想检索相关的MaterialPacking数据。我不知道怎么做 在上图中,我们看到材料包装为空,但尚未填充 以下是模型: public class TransportedMaterialPacking { public int TransportedMaterialPackingID { get; se
public class TransportedMaterialPacking
{
public int TransportedMaterialPackingID { get; set; }
public MaterialPacking MaterialPacking { get; set; }
public double Quantity { get; set; }
public double? Width { get; set; }
public double? Height { get; set; }
public double? Length { get; set; }
}
public class MaterialPacking
{
public int MaterialPackingID { get; set; }
public string DescriptionFr { get; set; }
public string DescriptionNl { get; set; }
}
关系是这样的:
运输材料>>运输材料包装>>材料包装
public static class ORMExtensions
{
public static IQueryable<T> MyInclude<T, C>(this IQueryable<T> source, Expression<Func<T, C>> function)
where C : class
where T : class
{
return source.Include(function);
}
我使用实体框架
谢谢。您可以使用.includenav1.nav2加载相关实体
我不知道.includex=>x.nav1.nav2是否有效您可以通过在查询中执行foo.IncludeNavigationProperty.SubNavigationProperty之类的操作来“级联”include
可以使用属性符号编辑以包含导航子属性,请参见示例。@DarinDimitrov:我更新了我的问题以显示我的包含代码。frookypoon,DarinDimitrov:当我尝试使用时。包含。。。在我的查询中,它让我在编译时出错。我不知道为什么,但Include有问题,但我的自定义函数MyInclude可以工作,但不适用于多级级联。@Bronzato我更新了答案,有关示例,请参见该链接。关键是使用Select。