C# 实体框架核心-包含集合属性的多个级别
我想知道如何在Entity Framework Core中包含集合的多个属性级别 我的情况的一个例子:C# 实体框架核心-包含集合属性的多个级别,c#,entity-framework-core,C#,Entity Framework Core,我想知道如何在Entity Framework Core中包含集合的多个属性级别 我的情况的一个例子: public class A { public ICollection<B> listB ... } public class B { public C c ... } 我只能访问ICollection本身(listB)的属性,而不能访问其中包含的B对象的属性,因此我可以在其中包含C对象 我设法手动完成了这项工作(比我希望的要详细得多),分别加载B对象并在其中包
public class A
{
public ICollection<B> listB ...
}
public class B
{
public C c ...
}
我只能访问ICollection本身(listB)的属性,而不能访问其中包含的B对象的属性,因此我可以在其中包含C对象
我设法手动完成了这项工作(比我希望的要详细得多),分别加载B对象并在其中包含我想要的内容,然后才将它们添加到a的listB中。然而,在我的现实生活中,我想在下面的级别中包含的属性也是用于集合的,因此这变得越来越不实用。是否有一种更简单、更优雅的方法来执行此操作?有两个重载
然后包括,一个用于前一个导航属性为单个实体的情况,另一个用于集合:
public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>([NotNullAttribute] this IIncludableQueryable<TEntity, TPreviousProperty> source, [NotNullAttribute] Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class;
public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>([NotNullAttribute] this IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>> source, [NotNullAttribute] Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class;
发件人:
当前版本的Visual Studio提供了不正确的代码完成选项,并且在集合导航属性之后使用ThenInclude方法时,可能会导致用语法错误标记正确的表达式。这是在上跟踪到的IntelliSense错误的症状。只要代码正确并且可以成功编译,就可以安全地忽略这些虚假的语法错误
@DiegoTorres谢谢你指出这一点,我把它添加到了我的答案中。
public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>([NotNullAttribute] this IIncludableQueryable<TEntity, TPreviousProperty> source, [NotNullAttribute] Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class;
public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>([NotNullAttribute] this IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>> source, [NotNullAttribute] Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class;
Context.AItems.Include(a => a.listB).ThenInclude(b => b.c)