1

我正在构建一个应用程序,我尝试在其中建立一个朋友系统。

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

第二:好友表如下:

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

在这个表中,“friendFrom”和“friendTo”都是字符串类型,并且接收注册用户的ID。

我想要实现的是,当我在视图上显示此表时,我想在“friendFrom”或“friendTo”列中显示相同用户 ID 的用户名。

4

1 回答 1

2

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

asp.net core的默认应用用户

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

于 2016-11-10T10:48:37.527 回答