2015-08-21 48 views
0

我有一個帶有實體框架的ASP.NET MVC 5代碼優先應用程序。我在查詢數據時遇到問題。。包含()不適用於在實體框架中重現ASP.NET MVC 5應用中的屬性

這是我的模型:

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; } 
} 

如果我嘗試使用此查詢

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

它只是返回與PasiScore財產空訪問來檢索與PasiScore一個訪問的數據。

在另一方面,如果我用這個查詢

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(); 

它工作正常。患者的訪問屬性適當地填充,並且每次訪問的PasiScore屬性也被正確加載,我不明白爲什麼它在第一種情況下不起作用。

邊注:db是定義爲

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; } 
    } 
+1

讓你所有的導航屬性'virtual' – trailmax

回答

相關問題