Я новичок в Entity Framework Core.
У меня такая структура данных:
Книги содержат много глав.
/>Главы содержат множество заметок на странице.
Мой запрос объекта в моем контроллере API выглядит следующим образом, и в ходе его отладки я вижу, что он находит правильные результаты.
ServiceResponse response = new ServiceResponse();
List Books = await _context.Books.Include(x => x.Chapters).ThenInclude(x => x.PageNotes).ToListAsync();
response.Data = Books;
response.Success = true;
response.Message = "List of All Books Provided";
return Ok(response);
Проблема в том, что я получаю следующее исключение в своем контроллере, когда он пытается вернуть данные.
System.InvalidOperationException: The exception handler configured on ExceptionHandlerOptions produced a 404 status response. This InvalidOperationException containing the original exception was thrown since this is often due to a misconfigured ExceptionHandlingPath. If the exception handler is expected to return 404 status responses then set AllowStatusCode404Response to true.
---> System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Data.Chapters.Book.Chapters.Book.Chapters.Book.Chapters.Book.Chapters.Book.Chapters.Book.Chapters.Book.Chapters.Book.Chapters.Book.Chapters.Id.
Любые советы приветствуются!
Если это актуально, вот мои классы базы данных. Я впервые создаю такую сложную структуру, так что проблема может быть именно в этом?
public class MyBooksTable
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
public ICollection UserBooks { get; set; }
[ForeignKey("UserId")]
public string UserId { get; set; }
public AppUser User { get; set; }
}
public class UserBook
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
[ForeignKey("MyBooksTableId")]
public string MyBooksTableId { get; set; }
public MyBooksTable MyBooksTable { get; set; }
[ForeignKey("BooksId")]
public string BooksId { get; set; }
public Book Book { get; set; }
public DateTime BookDateOfPurchase { get; set; } = DateTime.MinValue;
}
public class Book
{
[Key]
public string Id { get; set; } = Guid.NewGuid().ToString();
[Required]
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public decimal Price { get; set; } = decimal.Zero;
public ICollection Chapters { get; set; } = new List();
public ICollection? UserBooks { get; set; }
}
public class Chapter
{
[Key]
public string Id { get; set; } = Guid.NewGuid().ToString();
[Required]
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public ICollection? PageNotes { get; set; } = new List();
[ForeignKey("BookId")]
public string BookId { get; set; }
public Book Book { get; set; }
}
public class PageNote
{
[Key]
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
[ForeignKey("ChapterId")]
public string ChapterId { get; set; }
public Chapter Chapter { get; set; }
}
Подробнее здесь: https://stackoverflow.com/questions/782 ... om-web-api