【发布时间】:2021-04-12 15:50:19
【问题描述】:
您好,我无法与 ApplicationUser 类建立关系,您无法浏览!有人可以帮忙吗?
public class ApplicationUser : IdentityUser
{
public ICollection<Book> Book{ get; set; }
}
public class Book
{
public string UserID { get; set; }
[ForeignKey("UserID")]
public ApplicationUser ApplicationUser { get; set; }
}
在Controller中获取模型
// 获取:书籍 公共异步任务索引() {
var list = from s in _context.Books select s;
ApplicationUser ApplicationUser = await _userManager.GetUserAsync(HttpContext.User);
if (!await _userManager.IsInRoleAsync(ApplicationUser , "Admin"))
{
list = list.Where(s => s.ApplicationUser == ApplicationUser);
}
list = list.Where(s => s.State== BookUtils.States.ACTIVE);
return View(await list.AsNoTracking().ToListAsync());
}
当我尝试浏览模型 Book.cshtml @item.ApplicationUser = NULL ????
有人可以帮忙导航吗?
【问题讨论】:
-
您还没有向我们展示您是如何在控制器操作方法中获取模型的?
-
我添加了信息
-
好的,您没有在查询中获取用户。此外,您在这里拥有的 linq 是多余的。你可以直接
return View(await _context.Books.Include(b => b.ApplicationUser).ToListAsync()); -
我这样做是因为我必须应用过滤器,我已经在上面更新了。有没有更有效的方法来做到这一点?我必须做包含吗?不应该是自动的吗?
-
因为我的 Book 与另一个类有关系,所以我想做 Book.ApplicationUser.OtherClass.name 而不是我想担心包含依赖关系!
标签: asp.net asp.net-mvc asp.net-core