c# - 如何使用身份 aspnetusers
问题描述
我之前问过一个问题,但我认为我问的不正确。
在我的 asp.net mvc 应用程序中,我使用 aspnetusers 进行登录和注册。
如果我有另一个包含员工信息的员工模型,我如何连接这两个模型,以便当用户登录时,它会从员工表中获取他们的信息以供使用。我使用 sql server 创建了员工模型,并使用 ado.net 实体数据模型在 Visual Studio 中使用它。
员工模型:
public partial class Employee
{
public int UserID { get; set; }
[Key]
public int EmployeeID { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public System.DateTime StartDate { get; set; }
public int RoleID { get; set; }
public int ShiftID { get; set; }
public int AreaID { get; set; }
public int DisciplineID { get; set; }
public int SiteID { get; set; }
public int ALCategory { get; set; }
public int HoursTaken { get; set; }
public Nullable<int> AwardedLeave { get; set; }
public Nullable<int> TotalHoursThisYear { get; set; }
public int HoursCarriedForward { get; set; }
public Nullable<int> EntitlementRemainingThisYear { get; set; }
public string Comments { get; set; }
public int SickLeaveTaken { get; set; }
public Nullable<int> SickLeaveEntitlement { get; set; }
public Nullable<int> SickLeaveEntitlementRemaining { get; set; }
public int StudyLeaveEntitlement { get; set; }
public int StudyLeaveTaken { get; set; }
public Nullable<int> StudyLeaveRemaining { get; set; }
public int ExamLeaveTaken { get; set; }
public int ForceMajeure { get; set; }
public int BereavementLeaveTaken { get; set; }
public int MaternityLeaveTaken { get; set; }
public int ParentalLeaveTaken { get; set; }
public int AdoptionLeaveTaken { get; set; }
public string ManagerEmail { get; set; }
public string AreaManagerEmail { get; set; }
public virtual Area Area { get; set; }
public virtual Discipline Discipline { get; set; }
public virtual Shift Shift { get; set; }
public virtual Site Site { get; set; }
public virtual Employee Employee1 { get; set; }
public virtual Employee Employee2 { get; set; }
}
和登录模型:
public class LoginViewModel
{
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
那么我可以连接这两个吗?我使用数据库优先模型是否重要?我可以使用外键通过电子邮件连接吗?
例如,我想使用它来过滤员工表,以仅显示与登录用户具有相同站点 ID 的员工。
因此,当用户登录时,我希望他们只在员工 HTML 表上看到与自己具有相同站点的员工。
解决方案
成功登录后,您将获得 AspNetUser 的 Id。从 Employee 表中的 AspNetUser 表中获取列 Id 的外键引用。通过这样做,您可以达到预期的效果。只需在 Employee 模型中添加一件事,如下所述:
public virtual ApplicationUser ApplicationUser { get; set; }