【问题标题】:ASP.NETCore - Friends SystemASP.NET Core - 好友系统
【发布时间】:2017-03-24 08:40:35
【问题描述】:

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

现在,我为此使用了 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 的用户名。

【问题讨论】:

  • 好的,这听起来像是一个相当简单的查询 - 你已经尝试过任何东西了吗?您是否将数据库连接到 .NET,例如通过实体框架?
  • 嗯,我对 ASP.NET 很陌生,我在 PHP 中还可以,我认为它一定很简单,但后来我尝试了很多对我来说不合适的东西,仍在尝试解决这个问题:)
  • 如果您的问题基本上是“我不知道如何使用来自 ASP.NET 或 ASP.NET Core 的数据库”,那么我担心它对于 Stack Overflow 来说太宽泛了 - 但是有 很多教程、指南、截屏视频等。我建议你从这些开始 - Stack Overflow 是针对更具体的问题。
  • (我还强烈建议您了解并遵循 .NET 命名约定。)

标签: mysql asp.net linq system social-network-friendship


【解决方案1】:

你需要改变你的类如下(我没有测试这个):

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

【讨论】:

  • 如果您也可以与我分享控制器/视图,我将不胜感激:)
  • @Ben:已更改帖子
猜你喜欢
  • 2011-08-16
  • 2011-04-29
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2020-09-01
  • 2011-08-30
  • 2020-09-01
  • 1970-01-01
相关资源
最近更新 更多