首页 > 解决方案 > ASP.Net Core API:如何将用户身份链接到联系人

问题描述

我正在开发一个利用 ASP.Net Core 在 SQL 中的用户/角色身份表的应用程序。

由于我现在正在构建的应用程序的性质,公开注册是不可能的。我要构建的内容需要应用程序的管理员首先将新员工作为联系人添加到应用程序中。保存他们的联系信息后,他们将单击一个按钮将该联系人设置为用户。

您推荐什么方法来扩展身份框架或将 AspNetUsers 与我的联系人表链接以实现此目标。我确信我会经常从这个设置的双方查询信息,这是我提出这个问题的主要原因。

非常感谢您的帮助。如果我可以提供更多详细信息,请告诉我。

public class ApplicationUser : IdentityUser
{
    public virtual ICollection<Contact> Contacts { get; set; }
}
public class Contact
{
    [Key]
    public int Id { get; set; }
    public string IdentityUser { get; set; }
    [ForeignKey("IdentityUser")]
    public virtual ICollection<ApplicationUser> AppUser { get; set; }
}

标签: asp.net-coreasp.net-identity

解决方案


AContact可以在没有 的情况下存在,User但 aUser不能在没有 的情况下存在Contact。简单的一对一关系:

public class User : IdentityUser<int>
{
    int ContactId { get; set; }
    Contact Contact { get; set; }
}

public class Contact
{
    int? UserId { get; set; }
    User User { get; set; }
}

推荐阅读