0
我有3個嵌套模型:ApplicationUser(來自實體框架),City和State。 應用程序用戶具有城市作爲外鍵,城市具有作爲外鍵的狀態。 當我查詢一個用戶時,我得到一個包含City在內的所有屬性的用戶作爲相關模型,但是當我查找城市時,相關模型State爲null,所有其他屬性都可以。任何線索?嵌套實體模型查詢問題
這是StateModels
public class StateModels
{
public int Id { get; set; }
public string State { get; set; }
public string Abbreviation { get; set; }
}
這是CityModels
public class CityModels
{
public int Id { get; set; }
public string City { get; set; }
public int ZipCode { get; set; }
public virtual StateModels State { get; set; }
}
這是ApplicationUser
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string CompanyName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public virtual CityModels City { get; set; }
public string CompanyPhone { get; set; }
public string CompanyFax { get; set; }
public bool Validated { get; set; }
public bool Staff { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
這是我試圖去的狀態對象
ApplicationUser applicationUser = db.Users.Find(idUser);
var city = applicationUser.City; //object city is ok
var state = city.State; // this field is null, all others attributes are ok
在db中,所有的城市寄存器都有狀態ID參考