ASP.NETCore - 好友系统

ASP.NETCore - Friends System

我正在构建一个应用程序,尝试创建一个好友系统。

现在,我为此使用了 2 tables。 第一个:我存储用户信息的默认 AspNetUsers。

第二名:朋友Table如下:

    public class AspNetFriends
{
    public int ID { get; set; }
    public string friendFrom { get; set; }
    public string friendTo { get; set; }
    public bool isConfirmed { get; set; }
}

在这个table中"friendFrom"和"friendTo"都是字符串类型,接收注册用户ID。

我想要实现的是,当我在视图中显示此 table 时,我想显示 "friendFrom" 或 "friendTo" 中相同用户 ID 的用户名柱子。

您需要按照以下方式更改您的 class(我没有对此进行测试):

asp.net 核心的默认应用程序用户

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace project.Models
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser
    {

    }
}

型号

public class AspNetFriends
{
    public int ID { get; set; }
    public bool isConfirmed { get; set; }
    public virtual ApplicationUser friendFrom { get; set; }
    public virtual ApplicationUser friendTo { get; set; }
}

现在您可以访问 aspnet 用户的 getter 和 setter

控制器

public async Task<IActionResult> Details(int id)
{
    var query = from m in _dbContext.AspNetFriends
                    join ff in _dbContext.Users on
                        new { m.friendFrom.Id } equals new { Id = cu.Id }
                    join ft in _dbContext.Users on
                        new { m.friendTo.Id } equals new { Id = cu.Id }
                        where m.ID == id
                        select m;

    return View(query.Single());
 }

查看

@model project.AspNetFriends

<p>
@model.friendFrom.UserName
</P>

@item.CreationUser.UserName