如何制作书籍/书籍类别?

How Can I Make a Book / Book Category?

TblBook.cs

public class TBLBook : IEntity
{
    [Key]
    public int BookId { get; set; }
    public string BookName { get; set; }

    [ForeignKey("TblCategory")]
    public int CategoryId { get; set; }
    public int WriterId { get; set; }
    public string BookYearofPublication { get; set; }//Basım Yılı
    public string BookPublishingHouse { get; set; } //Yayın evi
    public string BookPage { get; set; }
    public bool BookStatus { get; set; }

    public TBLCategory TblCategory { get; set; }
}

TblCategory.cs

public class TBLCategory : IEntity
{
    [Key]
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }

    public  virtual ICollection<TBLBook> TblBooks { get; set; }
}

我这样做是因为它是企业架构。

public class EfEntityRepositoryBase<TEntity, TContext> : IEntityRepository<TEntity>
        where TEntity : class, IEntity, new()
        where TContext : DbContext, new()
    {
        ////return _context.Categories.Include(i => i.SubCategories).ToList();
        public List<TEntity> GetList(Expression<Func<TEntity, bool>> filter=null)
        {
            using (TContext context = new TContext())
            {
                return filter == null ? context.Set<TEntity>().ToList() : context.Set<TEntity>().Where(filter).ToList();
            }
        }
}

企业:

public List<TBLBook> GetList()
{
    return _bookDal.GetList();
}

控制器视图

public IActionResult Index()
{
    var query = new BookListViewModel
    {
        TblBooks = _bookService.GetList()
    };
    return View(query);
}

索引视图:

<tr>
    <td>@b.BookId</td>
    <td>@b.BookName</td>
    <td>@b.WriterId</td>
    <td>@b.CategoryId</td>     <!--Problem: @b.TblCategory.CategoryName-->
    <td>@b.BookYearofPublication</td>
    <td>@b.BookPublishingHouse</td>
    <td>@b.BookPage</td>
    <td>@b.BookStatus</td>
</tr>

我的问题是; 当我这样做时 @b.TblCategories.CategoryName

抛出空值或错误。

无法显示相应的类别名称。怎么填,谢谢 我的英语不好,对不起。我试着用图片来解释。

简而言之,这是我想要做的: 出现类别 ID。我希望它显示为类别名称。但是它看起来是空白的,我应该填充什么。

截图:

默认情况下不加载相关实体。

EF6:Loading Related Entities

EF 核心:Loading Related Entities

也不要在您的数据库或代码中使用“TBL”前缀,也不要将 DbContext 包装在额外的存储库层中。它已经是一个存储库。

所以加载这样的查询:

var books = db.Books.Include(b => b.Category).ToList();