2016-11-10 75 views
0

我正在建立一个应用程序,我尝试创建一个朋友系统。ASP.NETCore - 朋友系统

现在,我为2表工作。 1st:默认的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”栏。

+0

好了,这听起来像一个相当简单的查询 - 你有尽可能尝试什么都没有?你有没有把你的数据库连接到.NET,例如通过实体框架? –

+0

那么我很新的ASP.NET,即时通讯在PHP中,我认为它必须是简单的,但然后我很多事情,没有看起来对我好,仍然试图找出这一个:) –

+0

如果你的问题基本上是“我不知道如何使用ASP.NET或ASP.NET Core的数据库”,那么恐怕它对于Stack Overflow来说太广泛了 - 但是有很多的教程,指南,屏幕录像等。我建议你从这些开始 - 堆栈溢出是为了更具体的问题。 –

回答

0

您需要为随后改变你的类(我没有测试这一点):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用户的获取者和设置者

控制器

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

+0

如果你可以与我分享控制器/视图,我将不胜感激:) –

+0

@Ben:changed post – Wouter