我有一个用户登录然后它调用控制器。我在用户表中搜索电子邮件匹配并将其保存到用户变量中。但是当我尝试检查它是否是管理员用户时,权限列为空,它是空引用。
为什么用户不首先或默认提取所有列数据,我检查数据库并且permissionId在那里,但是当我检查所述行的值时将其拉为空。
我真的很清楚,这是我的第一个更大的项目,也是我第一次似乎无法在谷歌上找到答案。
这是实体:
用户:
public class User
{
public int UserId { get; set; }
[Column(TypeName = "VARCHAR(100)")]
[StringLength(100)]
[Required]
public string FirstName { get; set; }
[Column(TypeName = "VARCHAR(100)")]
[StringLength(100)]
[Required]
public string LastName { get; set; }
[Column(TypeName = "VARCHAR(100)")]
[StringLength(250)]
[Required]
public string Email { get; set; }
[Required]
public string Password { get; set; }
[Required]
public string UserName { get; set; }
//The user can have many posts
public List<Post> Post { get; set; }
public virtual Permissions Permissions { get; set; }
}
权限:
public class Permissions
{
public int PermissionsID {get; set;}
[Required]
public bool admin { get; set; }
[Required]
public bool StandUser { get; set; }
//Many Users can have a permission, this makes the foreign key for the Post Table.
public List<User> User { get; set; }
}
这是登录后控制器:
[HttpPost]
public IActionResult Login(LoginViewModel loginViewModel)
{
//We're grabbing the plain text password so we can hash it later.
loginViewModel.plainTextPassword = loginViewModel.user.Password;
//Searching for the users emaill address and gathering the user information.
User userInfo = _context.Users.FirstOrDefault(x => x.Email == loginViewModel.user.Email);
我希望 userInfo 的所有行都不为空,以便稍后我可以检查用户是否是管理员。
qq_笑_17
相关分类