简体   繁体   English

ASP.NET和EF Core Web API为对象内的对象返回null

[英]ASP.NET and EF Core Web API returning null for an object within an object

I'm using web api to return the data of a Book stored in an SQlLite database, using EF Core, on ASP.NET Core MVC. 我正在使用Web API在ASP.NET Core MVC上使用EF Core返回存储在SQlLite数据库中的Book数据。

Here is the DbContext code: 这是DbContext代码:

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Yara.SQLite
{
    public class BookingContext : DbContext
    {
        public DbSet<Book> Books { get; set; }
        public DbSet<Author> Authors { get; set; }

        //public DbSet<Author> Authors { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=books.db");
        }
    }

    public class Book
    {
        public int BookId { get; set; }
        public string Name { get; set; }

        public Author Author { get; set; }
    }

    public class Author
    {
        public string Name { get; set; }

        public int AuthorId { get; set; }
    }
}

and here is the web api controller code: 这是Web api控制器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Yara.SQLite;


namespace Yara.Controllers.API
{
    public class UsersController : Controller
    {
        [HttpGet]
        [Route("api/books")]
        public IEnumerable<Book> GetBooks()
        {
            using(var c = new BookingContext())
            {
                return c.Books.ToList();
            }
        }

        [HttpGet]
        [Route("api/books/{id:int}")]
        public Book GetBook(int id)
        {
            using(var c = new BookingContext())
            {
                return c.Books.FirstOrDefault(b => b.BookId == id);
            }
        }

        [HttpPost]
        [Route("api/books")]
        public Book AddBook([FromBody] Book book)
        {
            using(var db = new BookingContext())
            {
                db.Books.Add(book);
                db.SaveChanges();
                return db.Books.Where(o => o == book).FirstOrDefault();
            }
        }
    }
}

Both the GET requests seem to work fine, and so does the POST. 两种GET请求似乎都可以正常工作,而POST也可以。 When I want to add a new Book, I'll send a POST request, like so: 当我想添加一本新书时,我将发送一个POST请求,如下所示:

https://gyazo.com/e96c81479f7ccc084401d70cf13c4cbe https://gyazo.com/e96c81479f7ccc084401d70cf13c4cbe

and when i send that request, I'll get my data, like so: 当我发送该请求时,我将得到我的数据,如下所示:

{
    "bookId": 3,
    "name": "A book on US Politics",
    "author": {
        "name": "Enra",
        "authorId": 3
    }
}

When I try to do a GET request on /api/books/3 for example, it'll return this data: 例如,当我尝试在/ api / books / 3上执行GET请求时,它将返回以下数据:

{
    "bookId": 3,
    "name": "A book on US Politics",
    "author": null
}

I've even checked the database using SQLLite browser and the data exists. 我什至使用SQLLite浏览器检查了数据库,并且数据存在。 It doesn't make sense to me. 对我来说这没有意义。

EF Core uses Lazy Loading to improve performance. EF Core使用延迟加载来提高性能。 Essentially, unless you explicitly say to include the values, they're null. 本质上,除非您明确地说要包括这些值,否则它们为空。

To include your author information, change your get method to this: 要包括您的作者信息,请将您的get方法更改为此:

return c.Books.Include(b=>b.Author).FirstOrDefault(b => b.BookId == id);

You can also disable Lazy loading in its entirety, if you want. 如果需要,还可以完全禁用延迟加载。 You can find more information on this here: https://docs.microsoft.com/en-us/ef/core/querying/related-data 您可以在此处找到有关此的更多信息: https : //docs.microsoft.com/zh-cn/ef/core/querying/related-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM