0

I have an ASP.NET MVC 5 code-first application with Entity Framework. I have a problem when I make a query to retrieve data.

This are my models:

public class Patient
    {
        public int Id { get; set; }
        public int SubjectNumber { get; set; }
        public ICollection<AdverseEffect> AdverseEffects { get; set; }
        public InformedConsent InformedConsent { get; set; }
        public Demography Demography { get; set; }
        public PsoriasisHistory PsoriasisHistory { get; set; }
        public InfectionAssessment InfectionAssessment { get; set; }
        public TbInfectionAssessment TbInfectionAssessment { get; set; }
        public MedicalHistory MedicalHistory { get; set; }
        public ICollection<InclusionCriteria> InclusionCriterias { get; set;}
        public ICollection<ExclusionCriteria> ExclusionCriteria { get; set; }
        public PatientEligibilityReview PatientEligibilityReview { get; set;}
        public ICollection<Visit> Visits { get; set; }
        public AlcoholStatus AlcoholStatus { get; set; }
}

public class Visit
    {
        public int Id { get; set; }
        public int VisitNumber { get; set; }
        public string VisitName { get; set; }
        public ICollection<Record> DateOfVisit { get; set; }
        public AlcoholStatus AlcoholStatus { get; set; }
        public PhysicalExam PhysicalExam { get; set; }
        public Hematology Hematology { get; set; }
        public Biochemistry Biochemistry { get; set; }
        public UrineAnalysis UrineAnalysis { get; set; }
        public PasiScore PasiScore { get; set; }
    }

public class PasiScore
{
    public int Id { get; set; }
    public ICollection<Record> DateOfPasiScore { get; set; }
    public ICollection<Record> PasiScoreValue { get; set; }
}

If I try to retrieve data for a Visit with PasiScore using this query

        var visit =
            db.Visits.Where(v => v.Id == VisitId)
                .Include(v => v.Patient)
                .Include(v => v.PasiScore.DateOfPasiScore)
                .Include(v => v.PasiScore.PasiScoreValue)
                .FirstOrDefault();

it just returns a visit with the PasiScore property null.

On the other hand if I use this query

    var patient =
        db.Patients.Where(p => p.Id == PatientId)
        .Include(p => p.Visits.Select(v => v.PasiScore.DateOfPasiScore))
        .Include(p => p.Visits.Select(v => v.PasiScore.PasiScoreValue))
        .FirstOrDefault();

it works correctly. The Visits property of patient is properly populated and the PasiScore property of each visit is also properly loaded and I don't understand why it doesn't work in the first case.

Side note: db is an ApplicationDbContext defined as

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        public DbSet<Patient> Patients { get; set; }
        public DbSet<Visit> Visits { get; set; }
        public DbSet<Record> Records { get; set; }
        public DbSet<PasiScore> PasiScores { get; set; }
    }
Tieson T.
  • 20,774
  • 6
  • 77
  • 92

1 Answers1

0

I don't know for sure, but I think it might be because you try to populate multiple layers of navigation properties with plain Include() Functions in the first case, instead of using Select() in underlying as in 2nd and suggested here:

https://msdn.microsoft.com/en-us/library/gg671236(v=vs.103).aspx

DevilSuichiro
  • 1,049
  • 8
  • 21