Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 如何使用实体框架核心加载所有级别_Entity Framework_Entity Framework Core - Fatal编程技术网

Entity framework 如何使用实体框架核心加载所有级别

Entity framework 如何使用实体框架核心加载所有级别,entity-framework,entity-framework-core,Entity Framework,Entity Framework Core,我的域模型是: 书籍 public class Book { public int Id { get; set; } public string Name { get; set; } public int ReleaseYear { get; set; } public virtual ICollection<Store> Stores { get; set; } } 如何包含书籍的所有级别(和子级别) public Book GetBookByI

我的域模型是:

书籍

public class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ReleaseYear { get; set; }

    public virtual ICollection<Store> Stores { get; set; }
}
如何包含
书籍的所有级别(和子级别)

public Book GetBookById(int Id)
{
    return this._DbContext.Books
        .Include(p => p.Stores)
        .FirstOrDefault(p => p.Id == Id);
}
我试过什么

  • .thenclude(p=>p.StoreLocation)
    不起作用

    • 您必须按如下所示进行操作

        return this._DbContext.Books.Include(p => p.Stores).ThenInclude(q => q.Locations)
                          .FirstOrDefault(p => p.Id == Id);
      

      注意:有时VS无法正确显示智能。因此,请注意intellisense:D。一种解决方案可能是关闭VS并重新启动它的新实例。如果它不起作用,只需键入所需内容,而不必依赖智能。之后,它会编译并正常工作

      你试过了吗?是的。当一个
      列表
      在另一个
      列表
      中时,我不能包含第二个。它不起作用。在
      .thenclude(q=>q…
      上,它向我展示了列表的方法,我想是第一个。根据您的模型,它应该是一个列表否?那么有什么问题吗?不,它不起作用。是一个包含另一个列表的列表。我不能包含第二个列表(位置)。当使用
      时。然后包含
      我没有
      位置
      权限。相反,我有列表的泛型方法。泛型方法是什么意思?这是列表属性号?
      公共虚拟ICollection位置{get;set;}
      。那你为什么不能使用它呢?在你的模型中我可以这样使用。为什么你不能?那是因为VS上的
      智能性问题,没有考虑到这一点,只需键入我上面显示的代码并编译它。然后一切都会好起来。稍后你可以重新启动VS的新实例。
      
      public Book GetBookById(int Id)
      {
          return this._DbContext.Books
              .Include(p => p.Stores)
              .FirstOrDefault(p => p.Id == Id);
      }
      
        return this._DbContext.Books.Include(p => p.Stores).ThenInclude(q => q.Locations)
                          .FirstOrDefault(p => p.Id == Id);