EF Core DbContext not setting up my Models correctly for my DB schema

Keywords: c# sql entity-framework asp.net-core entity-framework-core


I have a Project entity that has a list property for a List<ProjectFileBucket> which is another Entity that contains a ProjectId and a list property for a List<ProjectFileBucketFiles> which is just a join table to join my files to their respective bucket. I'm doing something wrong in my model binding because I'm not getting back my actual ProjectFile object when executing my query, but I am getting back the ProjectFileId property. Perhaps my query is just wrong? Is it something in my DbContext? I'm at a loss here.

Here are my entity models: Project:

public class Project : IInt32Identity
    public int Id { get; set; }
    public string ProjectName { get; set; }
    public List<ProjectFileBucket> ProjectFileBuckets { get; set; }


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


public class ProjectFileBucketFiles
    public int ProjectFileId { get; set; }
    public ProjectFile ProjectFile { get; set; }
    public int ProjectFileBucketId { get; set; }
    public ProjectFileBucket ProjectFileBucket { get; set; }


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

Here is my 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)
            .HasKey(bc => new { bc.ProjectFileId, bc.ProjectFileBucketId });
            .HasOne(bc => bc.ProjectFileBucket)
            .WithMany(b => b.ProjectFiles)
            .HasForeignKey(bc => bc.ProjectFileBucketId);

And here is the query I'm trying to use to return my project:

public override Project GetById(int id)
        return _context.Projects
            .Where(x => x.Id == id)
            .Include(x => x.ProjectFileBuckets)
            .ThenInclude(b => b.ProjectFiles)

As I said above, when I check the debugger, my ProjectFileBucketFiles model has 3 of the 4 properties set, the one that's missing is the actual instance of ProjectFile (but the ProjectFileId property has a value).

I'm sure it's somethign simple, but I can't sort it out on my own.

any help would be greatly appreciated.