【问题标题】:Is it possible to reference another table so that an employee record is associated with a user record?是否可以引用另一个表以使员工记录与用户记录相关联?
【发布时间】:2019-06-07 16:50:16
【问题描述】:

我正在尝试构建一个网络应用程序,允许用户查看公司政策、程序、新闻通讯和他们自己的员工信息。

我的员工表包含所有员工信息(以及前缀、员工等级等相关的查找表)

我的应用程序将 asp-identity 用于登录功能,但我希望能够从人员表中返回仅与该特定用户相关的信息。

我知道可以扩展 ASP.net users 表以包含自定义字段,但这并不真正适合我的目标,因为管理团队在基于桌面的应用程序中使用了staff 表。

【问题讨论】:

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


    【解决方案1】:

    在您的员工表 UserId 中添加一个字段,例如 ALTER TABLE Staff ADD UserId NVARCHAR(256) DEFAULT NULL;

    您可以选择引用 AspnetUsers 表。

    更新 Staff 表中的行以将 UserId 值设置为相关的用户 ID(手动或创建操作来执行此操作)

    然后,在您的控制器中,您可以从员工的用户 ID 等于已连接 User.Identity.Id 的表中选择时事通讯。例如

    var news = context.Newsletters.Where(n=>n.Staff.UserId==User.Identity.Id);
    var infos = context.StaffInfos.Where(si=>si.Staff.UserId==User.Identity.Id);
    

    如果表格没有关系,您需要执行以下操作

    var employee = context.Staffs.FirstOrDefault(s => s.UserId == User.Identity.Id);
    if(employee != null) {
        var infos = context.StaffInfos.Where(si=>si.IdEmployee==employee.EmployeeId);
        return View(infos);
    } else {
        return Content("You don't have an account associated to your staff info...");
    }
    

    请将这些查询中的字段替换为您的字段名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多