0

I am use code first model with a relationship below

  public class ApplicationUser : IdentityUser
{
    public string UserFirstName { get; set; }
    public string UserLastName { get; set; }
    public string UserSchool { get; set; }

    public UserProfileData UserProfileData { get; set; }

    public int? MedicalSpecialtyId { get; set; }
    public virtual MedicalSpecialty MedicalSpecialty { get; set; }

  //  public int? AnalyticsDataId { get; set; }
 //   public ICollection<AnalyticsData> AnalyticsDatas { get; set; }
}

  public class MedicalSpecialty
{
    public int Id { get; set; }
    public string Description { get; set; }

  //  public int ApplicationUserId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }

    public ICollection<ProgramDetailData> ProgramDetailDatas { get; set; }
}

And when I try to get a User's associated MedicalSpecialty object it is NULL

 userSpecialtyName = currentUser.MedicalSpecialty.Description;

BUT when I run this code above it the currentUser.MedicalSpecialty is no longer NULL. What happened?? Somehow that LINQ query woke up the object and filled it with data

  var userSpecialtyId = currentUser.MedicalSpecialtyId;
            userSpecialtyName = _medicalSpecialtyRepository.Find
                                    (x => x.Id == userSpecialtyId).FirstOrDefault().Description;

            userSpecialtyName = currentUser.MedicalSpecialty.Description;
punkouter
  • 5,170
  • 15
  • 71
  • 116

0 Answers0