【问题标题】:I want to get item from a foreign key inside a foreign key (if that makes any sense...)我想从外键中的外键获取项目(如果这有任何意义......)
【发布时间】:2021-06-21 10:05:33
【问题描述】:

我正在使用 ASP.NET Core 标识。用户 ID 在 Invite 模型中将是 FK,我正在尝试显示邀请中的所有用户以及所需的信息。

我想在分配给用户的GameID 中显示GameName

所以它就像在邀请节目中GameName(用户中的FK)GameTag(用户)而不是带有数字的GameID

模型类:

public class Invite
{
    public int ID { get; set; }
    [ForeignKey("UserId")]    // ICollection<Invite> in User 
    [Display(Name = "Users")]
    public virtual ApplicationUser User { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public string Description { get; set; }
    [ForeignKey("GameID")] 
    public int? GameID { get; set; }
    public string GameTag { get; set; }
    public virtual ICollection<Invite> Invite { get; set; }
}

public class Game
{
    public int ID { get; set; }

    [Display(Name = "Game")]
    public string GameName { get; set; }
    public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }//Allow Users to get Games FKID (Foreign key ID)
}

在邀请控制器索引中获取邀请列表并将它们放入 viewbag 中以获取邀请剃刀索引页面。它只显示 GameID,它是用户内部的 FK,我不知道如何从分配给用户作为 FK 的 Invite 获取游戏 FK 内部的信息

// GET: Invites
public async Task<IActionResult> Index()
{
    ViewBag.InviteList = new List<String>();
    var invite = _context.Invites;
    var theuser = _context.ApplicationUser;

    foreach (Invite i in invite)
    {
         foreach (ApplicationUser tu in theuser)
         {
             if (i.User.Id == tu.Id)
             {
                 ViewBag.InviteList.Add(tu.GameID + " " +tu.GameTag);
             }
         }
    }

    return View(await _context.Invites.ToListAsync());
}

如果有人理解我的意思,欢迎提出更好的标题

【问题讨论】:

  • 只有 3 个模型类。 GameID 将作为 FK 插入到 User Model 中,而 User 将作为 FK 插入到 Invite 中。我想从邀请中获取用户中游戏名称的数据

标签: asp.net-mvc asp.net-core asp.net-mvc-5 asp.net-core-identity


【解决方案1】:

您的代码未正确实现(除了显示GameName 的主要要求)。实际上来自Game 的信息不是直接来自ApplicationUser 的引用。不知道你为什么不把它包括在课堂上(连同GameID)。假设你这样做(包括属性Game),代码可以像这样简单:

var invites = await _context.Invites.AsNoTracking()
                            .Include(e => e.User.Game).ToListAsync();

//NOTE: this can contain duplicate strings added to `InviteList`, unless you 
//include more data field in each item.
foreach (Invite i in invites)
{
     ViewBag.InviteList.Add(i.User.Game.GameName + " " + i.User.GameTag);         
}

如果您不想在ApplicationUser 中包含属性Game,则需要加入实体,如下所示:

var games = await (from invite in _context.Invites
                   join game in _context.Games on invite.User.GameID equals game.ID
                   select new { game.GameName, invite.User.GameTag }).AsNoTracking().ToListAsync();
//to avoid duplicate items (as noted in the previous code)
ViewBag.InviteList = games.GroupBy(e => e.GameName)
                          .Select(g => g.First())
                          .Select(e => $"{e.GameName} {e.GameTag}").ToList();

【讨论】:

  • public Game Game { get; set; } 放入 applicationUser 模型中。这包括财产吗?
  • @HelloHello 是的,就像您在 Invite 中包含 User 属性一样
  • @HelloHello 对不起,你需要包含User.Game,更新了代码。
猜你喜欢
  • 1970-01-01
  • 2020-12-18
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多