спасибо за вашу помощь
два класса:
public class Author
{
public int Id { get; set; }
public string Name { get; set; }
public List<AuthorBook> Books { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public List<AuthorBook> Authors { get; set; }
}
public class AuthorBook
{
public int AuthorId { get; set; }
public Author Author { get; set; }
public int BookId { get; set; }
public Book Book { get; set; }
}
DataContext
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
public DbSet<Book> Books { get; set; }
public DbSet<Author> Authors { get; set; }
// public DbSet<AuthorBook> AuthorBooks { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AuthorBook>()
.HasKey(t => new { t.AuthorId, t.BookId });
modelBuilder.Entity<AuthorBook>()
.HasOne(p => p.Author)
.WithMany(w => w.Books)
.HasForeignKey(h => h.AuthorId);
modelBuilder.Entity<AuthorBook>()
.HasOne(p => p.Book)
.WithMany(w => w.Authors)
.HasForeignKey(h => h.BookId);
}
}
Контроллер
[HttpGet]
public async Task<IActionResult> GetAuthors()
{
var authors = await this.context.Authors.Include(i => i.Books).ToListAsync();
return Ok(authors);
}
1012 * возвращается *
{
"id": 1,
"name": "Paul",
"books": [
{
"authorId": 1,
"bookId": 2,
"book": null
},
{
"authorId": 1,
"bookId": 5,
"book": null
}
]
}
Почему authorId, bookId в порядке, но не могут напрямую получить доступ к книге объектов (пусто) ???
Спасибо за вашу помощь