Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

c# - 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; }
    }

I did this because it is corporate architecture.

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();
            }
        }
}

Business:

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

Controller View

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

Index View:

<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>

My problem is; when i do this @b.TblCategories.CategoryName

throws null or error.

I cannot show the corresponding category name. how can I fill it, thanks. My English is bad, sorry. I tried to explain with pictures.

In short, here's what I want to do: The category ID appears. I want it to appear as the Category Name. but it looks blank, what should I fill with.

Screen-shot: enter image description here

question from:https://stackoverflow.com/questions/65902888/how-can-i-make-a-book-book-category

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Related entities are not loaded by default.

EF6: Loading Related Entities

EF Core: Loading Related Entities

Also don't use a "TBL" prefix in your database or your code, and don't wrap the DbContext in an extra repository layer. It's already a repository.

So load with a query like:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...