【发布时间】: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 表列中检索数据,如您在此屏幕截图中看到的:
这是我的桌子:
我正在尝试将 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; }
}
我也想知道?如果我没有看到我在此参考中所做的属性,如此屏幕截图所示:
【问题讨论】:
-
你在使用 .Net Core 吗?
-
不,我使用的是 Asp.net MVC 5。我将编辑这篇文章以添加此信息。
标签: asp.net asp.net-mvc entity-framework