在我的ASP.NET Core项目中,我拥有以下带有一些自定义属性的ApplicationUser
类,其中包括一个Address
类:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gendre { get; set; }
public string CityOfBirth { get; set; }
public string ProvinceOfBirth { get; set; }
public string FiscalCode { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }
public Address Address { get; set; }
public string AdditionaInfos { get; set; }
public LoginUserStatus Status { get; set; }
public string CountryOfBirth { get; set; }
}
public enum LoginUserStatus
{
Submitted,
OnHold,
Approved,
Rejected
}
地址类非常简单:
public class Address
{
public int ID { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public string Province { get; set; }
public string City { get; set; }
public string Fraction { get; set; }
public int PostalCode { get; set; }
public string Street { get; set; }
public int CivicNumber { get; set; }
public string CiviNumberAdditional { get; set;}
public string AdditionalInfo { get; set; }
}
在控制器中,我在列表中填充了将在视图中使用的项目,并使用一些Ajax代码获取了它们。我的控制器方法是:
[HttpGet]
public async Task<List<string>> RetrieveUser()
{
var currentUser = await _userManager.GetUserAsync(HttpContext.User);
List<string> currentUserItems = new List<string>();
currentUserItems.Add(currentUser.FirstName);
currentUserItems.Add(currentUser.LastName);
currentUserItems.Add(currentUser.Email);
currentUserItems.Add(currentUser.Gendre);
currentUserItems.Add(currentUser.CountryOfBirth);
currentUserItems.Add(currentUser.ProvinceOfBirth);
currentUserItems.Add(currentUser.CityOfBirth);
currentUserItems.Add(currentUser.DateOfBirth.ToString());
currentUserItems.Add(currentUser.FiscalCode);
currentUserItems.Add(currentUser.Address.Country);
return currentUserItems;
}
在dbo.AspNetUsers
中插入了阴影AddressID
。
我的问题是currentUser.Address
返回null
。
所有其他自定义属性(主要是字符串)都可以很好地返回。
如果我尝试使用Address
currentUser
对象中不包含类_userManager.GetUserAsync(HttpContext.User)
任何想法,我在做什么错了?