我有一个Project
实体,它有一个List<ProjectFileBucket>
的列表属性,这是另一个包含ProjectId
和一个List<ProjectFileBucketFiles>
的列表属性的实体,联接表,将我的文件联接到各自的存储桶。我在模型绑定中做错了事,因为执行查询时我没有取回实际的ProjectFile
对象,但是我取回了ProjectFileId
属性。也许我的查询是错误的?我的DbContext中有东西吗?我在这里茫然。
这是我的实体模型: 项目:
public class Project : IInt32Identity
{
public int Id { get; set; }
public string ProjectName { get; set; }
...
public List<ProjectFileBucket> ProjectFileBuckets { get; set; }
}
ProjectFileBucket:
public class ProjectFileBucket : IInt32Identity
{
public int Id { get; set; }
public List<ProjectFileBucketFiles> ProjectFiles { get; set; }
public int ProjectId { get; set; }
public DateTime CreatedDate { get; set; }
}
ProjectFileBucketFiles:
public class ProjectFileBucketFiles
{
public int ProjectFileId { get; set; }
public ProjectFile ProjectFile { get; set; }
public int ProjectFileBucketId { get; set; }
public ProjectFileBucket ProjectFileBucket { get; set; }
}
ProjectFile:
public class ProjectFile : IInt32Identity
{
public int Id { get; set; }
public string ProjectFileName { get; set; }
public string ProjectFilePath { get; set; }
public int ProjectId { get; set; }
public string ProjectFileType { get; set; }
public int ProjectFileViewedTimes { get; set; }
public bool ProjectFileActive { get; set; }
public string ProjectFileDescription { get; set; }
public int ProjectFileUserId { get; set; }
public DateTime ProjectFileCreatedDate { get; set; }
}
这是我的DbContext:
public DbSet<Project> Projects { get; set; }
public DbSet<ProjectFile> ProjectFiles { get; set; }
public DbSet<ProjectFileBucket> ProjectFileBuckets { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ProjectFileBucketFiles>()
.HasKey(bc => new { bc.ProjectFileId, bc.ProjectFileBucketId });
modelBuilder.Entity<ProjectFileBucketFiles>()
.HasOne(bc => bc.ProjectFileBucket)
.WithMany(b => b.ProjectFiles)
.HasForeignKey(bc => bc.ProjectFileBucketId);
}
这是我要用来返回项目的查询:
public override Project GetById(int id)
{
return _context.Projects
.Where(x => x.Id == id)
.Include(x => x.ProjectFileBuckets)
.ThenInclude(b => b.ProjectFiles)
.FirstOrDefault();
}
如上所述,当我检查调试器时,我的ProjectFileBucketFiles
模型具有4个属性集中的3个,而缺少的是ProjectFile的实际实例(但是ProjectFileId
属性有一个值)。
我敢肯定这很简单,但是我不能自己解决它。
任何帮助将不胜感激。
TIA