2016-08-07 5 views
0

저는 ApplicationUser (엔티티 프레임 워크에서), City 및 State의 3 가지 중첩 모델을 보유하고 있습니다. ApplicationUser에는 도시가 외래 키로 있고 도시에 외래 키로 상태가 있습니다. 사용자를 쿼리 할 때 도시를 관련 모델로 포함하는 모든 특성을 가진 사용자를 얻었지만 도시를 조회 할 때 관련된 모델 상태는 null이고 다른 모든 특성은 ok입니다. 어떤 단서?중첩 엔티티 모델 쿼리 문제

이 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 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 
에 도착하려고 어떻게 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; 
     } 
    } 

입니다

db에서 모든 도시 레지스터는 상태 ID 참조

답변

0

입니다. 시도해보십시오. db.Users.Find (idUser) .Include (u => u.City) .Include (u => u.City.State) 모든 외래 키를 올바르게 설정했는지 확인하십시오.