JSON中的HTTP GET结果具有空字段ASP.NET Core

azpvetkf  于 5个月前  发布在  .NET
关注(0)|答案(1)|浏览(115)

我目前正在学习如何编写一个ASP.NET Core Web API,我遇到了一个问题。我有一个非常原始的HTTP Get,它应该返回存储在DbSet中的所有书籍。当它返回它们时,所有存储其他对象或其他对象集合的字段,在本例中为Reviews,都被设置为NULL。
我试图通过Include()解决这个问题,但是在存储对象中已经存在空字段时也会出现同样的问题。
下面是ApiContext、User、Book、Review和DataController类:

public class ApiContext : DbContext
{
    public ApiContext(DbContextOptions<ApiContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Review>()
                    .Property(r => r.Description)
                    .IsRequired(false);

        modelBuilder.Entity<Book>()
                    .HasMany(b => b.Reviews)
                    .WithOne(r => r.Book_)
                    .HasForeignKey(r => r.BookID);

        modelBuilder.Entity<User>()
                    .HasMany(u => u.Reviews)
                    .WithOne(r => r.User_)
                    .HasForeignKey(r => r.UserID);                      
    }

    public DbSet<Book> Books { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Review> Reviews { get; set; }
}
public class User
{
    public int ID { get; set; }
    public string Nickname { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public ICollection<Review> Reviews { get; set; }
}

public class Book
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public string Description { get; set; }
    public int Pages { get; set; }
    public ICollection<Review> Reviews { get; set; }
}

public class Review
{
    public int ID { get; set; }
    public string Title { get; set; }       
    public string Description { get; set; }
    public int UserID { get; set; }
    public User User_ { get; set; }
    public int BookID { get; set; }
    public Book Book_ { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class DataController : ControllerBase
{
    private readonly ApiContext _context;

    public DataController(ApiContext context)
    {
        _context = context;
    }

    [HttpGet]
    public JsonResult AllBooks()
    {
        return new JsonResult(Ok(_context.Books.ToList()));
    }
}

结果如下:

{
  "value": [
    {
      "id": 1,
      "title": "Harry Potter and the Philosopher's Stone",
      "author": "J.K. Rowling",
      "description": "Book about Harry Potter's adventure 1.",
      "pages": 336,
      "reviews": null
    },
    {
      "id": 2,
      "title": "Harry Potter and the Chamber of Secrets",
      "author": "J.K. Rowling",
      "description": "...",
      "pages": 432,
      "reviews": null
    },
    {
      "id": 3,
      "title": "Harry Potter and the Prisoner of Azkaban",
      "author": "J.K. Rowling",
      "description": "Book about Harry Potter's adventure 3.",
      "pages": 523,
      "reviews": null
    }
  ],
  "formatters": [],
  "contentTypes": [],
  "declaredType": null,
  "statusCode": 200
}


我希望我不是唯一一个面临这个问题,我可以得到一些帮助来解决它。

91zkwejq

91zkwejq1#

你的代码应该获取相关的实体。EF有三种模式用于加载相关的数据。
例如,Include()方法作为急切加载模式的实现工作。

[HttpGet] 
public JsonResult AllBooks() 
{
    var books = _context.Books
        .Include(book => book.Reviews)
        .ToList()
    
    return new JsonResult(Ok(books));
}

字符串
您可以在下面获得更多信息:
https://learn.microsoft.com/en-us/ef/core/querying/related-data/

相关问题