如何使用Include获取项目模型中的项目列表

时间:2015-12-16 13:33:41

标签: c# lazy-loading eager-loading

enter code here我想在模型中显示AppPlatforms列表,我有4个表App,AppPlatforms,Reviews,AppsArtifacts他们有关系。所以我想要检索该App的App数据以及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();

然后我收到一个错误,即InnerException = {“'on clause'中的未知列'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; }
    }

1 个答案:

答案 0 :(得分:0)

也许你需要在映射中建立关系? help

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