【问题标题】:Why don't I see all AspNetUsers Table Columns when I try to query it using IdentityDbContext?当我尝试使用 IdentityDbContext 查询时,为什么看不到所有 AspNetUsers 表列?
【发布时间】:2019-05-20 22:45:40
【问题描述】:

我正在使用 EntityFramework 6.2 开发 ASP.NET MVC5 应用程序。

我有文章表和用户表。每篇文章都有一个我想从 AspNetUsers 表中检索的作者。我通过修改 RegisterViewModel 并添加 UserName、FullName、Description 向该表添加了新列域模型的属性。

问题我看不到我添加的这个新属性,但我可以使用这些行从 AspNetUsers 表中看到 UserId 和 UserName。

User.Identity.GetUserId();

User.Identity.GetUserName();


然后我搜索了我尝试使用 IdentityDbContext 并通过这样做,我可以从除 (UserName, FullName, Description) 之外的所有 AspNetUsers 表列中检索数据,如您在此屏幕截图中看到的:

query

这是我的桌子:

table

我正在尝试将 FullName 分配给文章作者,如下所示:

var AspNetUser = new IdentityDbContext();
var UserInfo = AspNetUser.Users.Where(u => u.Id == article.UserId);
article.AuthorName = UserInfo.FullName;

RegisterViewModel 域模型:

public class RegisterViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [Display(Name="Full Name")]
    public string FullName { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    [Display(Name = "User Name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

文章领域模型:

public class Article
{
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Body { get; set; }
    public string UserId { get; set; }
    public string AuthorName { get; set; }
    public ApplicationUser User { get; set; }
}

我也想知道?如果我没有看到我在此参考中所做的属性,如此屏幕截图所示:

IdentityUser Properties

【问题讨论】:

  • 你在使用 .Net Core 吗?
  • 不,我使用的是 Asp.net MVC 5。我将编辑这篇文章以添加此信息。

标签: asp.net asp.net-mvc entity-framework


【解决方案1】:

我发现通过使用 IdentityDbContext,我无法查询所有 AspNetUsers 表列。使用 IdentityDbContext,您将能够从原始表列而不是您添加的列中获取数据。

假设您想从 AspNetUsers 表中获取 电话号码。您可以使用 IdentityDbContext 获取它,因为该列最初是随 AspNetUsers 表一起提供的。

  ApplicationDbContext Context = new ApplicationDbContext();

  var CurrentUser = User.Identity.GetUserId();

  string PhoneNumber = Context.Users
 .Single(u => u.Id == CurrentUser)
 .PhoneNumber;

但是如何查询 AspNetUsers Table 并从您添加的列中获取信息?这里是获取 FullNameDescription 的示例当前登录的用户。

首先添加这个命名空间:

using Microsoft.AspNet.Identity.Owin;

然后

string FullName = HttpContext.GetOwinContext()
                .GetUserManager<ApplicationUserManager>()
                .FindById(User.Identity.GetUserId()).FullName;

string ProfileDescription = HttpContext.GetOwinContext()
                .GetUserManager<ApplicationUserManager>()
                .FindById(User.Identity.GetUserId()).Description;

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 2016-03-10
    • 2019-11-04
    • 1970-01-01
    • 2021-07-10
    • 2019-06-05
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    相关资源
    最近更新 更多