【发布时间】:2020-05-30 19:45:47
【问题描述】:
我目前正在从事图书馆管理项目,用户在借书时会更新他们的阅读状态。当我使用 linq 连接表时,除了状态之外的所有其他内容都在那里。 这是我的模型: 用户模型(TblUser):
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
namespace LibMan.Models.DB
{
public partial class TblBookStatus
{
public int ResId { get; set; }
public string UserEmail { get; set; }
public int? BookId { get; set; }
public string Status { get; set; }
public virtual TblBook Book { get; set; }
public virtual TblUser User { get; set; }
}
}
图书模型(TblBook):
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace LibMan.Models.DB
{
public partial class TblBook
{
public int BookId { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Translator { get; set; }
public string Publisher { get; set; }
public string Description { get; set; }
public string Category { get; set; }
public byte[] Cover { get; set; }
}
}
图书状态模型(TblBookStatus):
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
namespace LibMan.Models.DB
{
public partial class TblBookStatus
{
public int ResId { get; set; }
public string UserEmail { get; set; }
public int? BookId { get; set; }
public string Status { get; set; }
public virtual TblBook Book { get; set; }
public virtual TblUser User { get; set; }
}
}
继续下面的代码,您可以看到我使用 linq 查询连接创建了 DisplayBook,但我无法在 html 端显示状态。它说它
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LibMan.Models.DB;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
namespace LibMan.Pages
{
public class LibraryModel : PageModel
{
private readonly LibMan.Models.DB.LibManContext _context;
public string Message { get; set; }
public LibraryModel(LibMan.Models.DB.LibManContext context)
{
_context = context;
DisplayBook = new List<TblBook>();
; }
public IList<TblBookStatus> TblBookStatus { get; set; }
public IList<TblBook> TblBook { get; set; }
public IList<TblBook> DisplayBook { get; set; }
public IList<TblUser> TblUser{ get; set; }
[BindProperty]
public async Task OnGetAsync()
{
TblBookStatus = await _context.TblBookStatus.ToListAsync();
TblBook = await _context.TblBook.ToListAsync();
TblUser = await _context.TblUser.ToListAsync();
var UserEmail = HttpContext.Session.GetString("Email");
if (TblBookStatus != null && TblBook != null)
{
DisplayBook = (from book in TblBook
join stat in TblBookStatus
on book.BookId equals stat.BookId
join usr in TblUser
on stat.UserEmail equals usr.Email
where (usr.Email == UserEmail)
select book).ToList();
}
}
}
}
我该怎么做?我可以在将 DisplayBook 分配给 Linq 查询结果的语句中执行此操作吗?做这个的最好方式是什么?谢谢! 注意:如果需要,我可以提供 html 代码。
【问题讨论】:
-
TblBookStatus具有可为空的BookId,为什么会这样?因为看起来这张表只是书籍和用户之间的多对多。将public string Status { get; set; }移动到单独的表并TblBookStatus引用它会更有意义。 -
你想用
DisplayBook显示什么? -
TblBookStatus 上的 BookId 作为数据库上的外键进行管理,这就是它可以为空的原因,它是在我设置项目时自动生成的。在 displaybook 声明中,当用户将一本书添加到他们的图书馆时,我会在该页面上显示它,我会显示图书信息。我还想显示不在 DisplayBook 中的 TblBookStatus 的状态。一位用户拥有一本书。正如你所说,这个项目适用于许多用户和书籍。
-
一个用户只能拥有一本书?
-
你能添加你的
User类吗,TblBookStatus重复了两次。