Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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 Core_Ef Core 2.1 - Fatal编程技术网

C# 在集合中的每个实体中包括映射表

C# 在集合中的每个实体中包括映射表,c#,entity-framework-core,ef-core-2.1,C#,Entity Framework Core,Ef Core 2.1,我有以下表格: public class Author { public int Id { get; set; } [Required] public string Name { get; set; } public ICollection<Book> Books { get; set; } = new List<Book>(); } public class Book { public int Id { get; set;

我有以下表格:

public class Author
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    public ICollection<Book> Books { get; set; } = new List<Book>();
}

public class Book
{
    public int Id { get; set; }

    [Required]
    public string Title { get; set; }

    public int AuthorId { get; set; }

    public Author Author { get; set; }

    public ICollection<BookBorrower> BookBorrowers { get; set; } = new List<BookBorrower>();
}

public class BookBorrower
{
    public int Id { get; set; }

    public int BookId { get; set; }

    public Book Book { get; set; }

    public int BorrowerId { get; set; }

    public Borrower Borrower { get; set; }

    public DateTime StartDate { get; set; }

    public DateTime? EndDate { get; set; }

    public bool IsBookReturned { get; set; } = false;
}
作者包含其所有的书,但每本书都不包括其bookbookers属性

author.Books.BookBorrowers.Count // that's zero but I have enities in those table for those book.
您需要使用,例如:

var author = await _db.Authors
            .Include(a => a.Books)
            .ThenInclude(b => b.BookBorrowers) //Add this line
            .FirstOrDefaultAsync(a => a.Id == id);

您还可以执行
.Include(“Books.bookbookers”)
在one@Chronicle是的,但是这种方法没有类型安全性,所以我永远不会推荐它。天哪,非常感谢你。。。。事实上,我试过使用thenclude,但根据intellisens的说法,没有这样的属性,但当我填写属性的全名时,它就起作用了;o
var author = await _db.Authors
            .Include(a => a.Books)
            .ThenInclude(b => b.BookBorrowers) //Add this line
            .FirstOrDefaultAsync(a => a.Id == id);