2015-12-16 88 views
0

enter code here我想顯示模型中的AppPlatforms列表,我有4個表格App,AppPlatforms,Reviews,AppsArtifacts他們有關係。所以我想要檢索應用程序的數據以及該應用程序的AppPlatforms(如iOS和Android),然後在AppPlatforms中收藏評論和AppArtifacts。如何獲取項目模型中使用的項目列表包括

我已經使用這個代碼,以包括

當我使用此

var app = dbContext.Apps 
        .Include("AppPlatforms.Reviews") 
        .Where(a => a.AppId == appId).ToList(); 

OR

var app = dbContext.Apps 
        .Include("AppPlatforms.Artifacts") 
        .Where(a => a.AppId == appId).ToList(); 

單獨它被包括在內,但是當我使用此

var app = dbContext.Apps 
        .Include("AppPlatforms.Reviews") 
        .Include("AppPlatforms.Artifacts") 
        .Where(a => a.AppId == appId).ToList(); 

然後我走了t錯誤,即InnerException = {「on子句'中的」未知列'Extent1.AppId'}}

這是我的表格。

public class App 
    { 
     [Key] 
     public int AppId { get; set; } 
     public string Name { get; set; } 
     public bool? IsActivated { get; set; } 
     public bool? Status { get; set; } 
     public string UserId { get; set; } 
     public DateTime UploadedDate { get; set; } 
     [ForeignKey("UserId")] 
     public virtual IdentityUser IdentityUser { get; set; } 

     public int? CategoryId { get; set; } 
     [ForeignKey("CategoryId")] 
     public virtual Category Category { get; set; } 
     public int? SubCategoryId { get; set; } 
     [ForeignKey("SubCategoryId")] 
     public virtual SubCategory SubCategories { get; set; } 

     public virtual ICollection<AppPlatform> AppPlatforms { get; set; } 

    } 


public class AppPlatform 
    { 
     [Key] 
     public int AppsPlatformId { get; set; } 
     public string AlternativeName { get; set; } 
     public string Version { get; set; } 
     public string DownloadLink { get; set; } 
     public DateTime LastUpdated { get; set; } 
     public DateTime? ReleaseDate { get; set; } 
     public string Description { get; set; } 
     public bool IsActive { get; set; } 
     public float FileSize { get; set; } 
     [DefaultValue(0)] 
     public int AppId { get; set; } 
     [ForeignKey("AppId")] 
     public virtual App App { get; set; } 
     public int PlatformId { get; set; } 
     [ForeignKey("PlatformId")] 
     public virtual Platform Platforms { get; set; } 
     public virtual ICollection<Review> Reviews { get; set; } 
     public virtual ICollection<AppsArtifact> Artifacts { get; set; } 

    } 


public class AppsArtifact 
    { 
     [Key] 
     public int Id { get; set; } 
     public int AppsPlatformID { get; set; } 
     [ForeignKey("AppsPlatformID")] 
     public virtual AppPlatform AppPlatform { get; set; } 

     public int ArtifactId { get; set; } 

     [ForeignKey("ArtifactId")] 
     public virtual Artifact Artifact { get; set; } 
     public AppArtifactType Type { get; set; } 
    } 

public class Review 
    { 
     [Key] 
     public int ReviewId { get; set; } 
     public string Description { get; set; } 
     public int Value { get; set; } 
     public int Risk { get; set; } 
     public ReviewStatus Status { get; set; } 
     public DateTime ReviewDate { get; set; } 

     public int? AppPlatformId { get; set; } 
     [ForeignKey("AppPlatformId")] 
     public virtual AppPlatform AppPlatform { get; set; } 

     public string ReviewerId { get; set; } 

     [ForeignKey("ReviewerId")] 
     public virtual IdentityUser IdentityUser { get; set; } 
     public int LevelId { get; set; } 
     [DefaultValue(1)] 
     public bool IsActive { get; set; } 


     [ForeignKey("LevelId")] 
     public virtual Level Levels { get; set; } 
    } 

回答

0

也許你在映射中需要關係? help

// Relationships 
      this.HasMany(t => t.AppPlatforms) 
       .WithMany() 
       .Map(m => 
       { 
        m.ToTable("Table_AppPlatform"); 
        m.MapLeftKey("IdLeft"); 
        m.MapRightKey("IdRight"); 
       }); 
+0

的關係用'ForeignKey'屬性 –

+0

也許是不夠的映射 – J4ime