Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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
C# 如何在多对多关系的实体框架中获得深层关系?_C#_Entity Framework - Fatal编程技术网

C# 如何在多对多关系的实体框架中获得深层关系?

C# 如何在多对多关系的实体框架中获得深层关系?,c#,entity-framework,C#,Entity Framework,我有以下多对多关系: PersonBook; BookAuthor; BookGenre; 我可以得到一个人的书,但它不包含作者和流派列表 我为每个类创建了一个模型,并将实体对象映射到该模型 EF6中的解决方案: Person person = db.Person .Where(p => p.PersonID == 1) .Include(p => p.Books.Select(b => b.Author

我有以下多对多关系:

PersonBook; BookAuthor; BookGenre; 
我可以得到一个
,但它不包含
作者
流派
列表

我为每个类创建了一个模型,并将实体对象映射到该模型

EF6中的解决方案:

 Person person = db.Person
                 .Where(p => p.PersonID == 1)
                 .Include(p => p.Books.Select(b => b.Authors))
                 .Include(p => p.Books.Select(g => g.Genres))
                 .FirstOrDefault();

要在Entity FrameworkCore中获取相关数据,最佳选择是使用
Include()
thenclude()
方法:

Person person = db.Person
                .Where(p => p.PersonID == 1) // change this
                .Include(ba => ba.BookAuthor.Select(a => a.Author)
                .Include(bg => bg.BookGenre.Select(g => g.Genre)
                .First();

要在Entity FrameworkCore中获取相关数据,最佳选择是使用
Include()
thenclude()
方法:

Person person = db.Person
                .Where(p => p.PersonID == 1) // change this
                .Include(ba => ba.BookAuthor.Select(a => a.Author)
                .Include(bg => bg.BookGenre.Select(g => g.Genre)
                .First();

我很惊讶它居然还书。根据你的询问,我想说你没有包括在内。您使用的是EF还是EF Core?我很惊讶它居然还书。根据你的询问,我想说你没有包括在内。您使用的是EF还是EF Core?EFI没有BookAuthor类。我无法从包含的人那里找到书籍,但它带有select。这里是我的EF生成的Book构造函数;```public Person(){this.Books=new HashSet();}公共虚拟ICollection Books{get;set;}``我没有BookAuthor类。我无法从包含的人那里找到书籍,但它带有select。这里是我的EF生成的Book构造函数;```public Person(){this.Books=new HashSet();}公共虚拟ICollection Books{get;set;}```