Entity Framework Core Joining Related Table

asp.net-mvc c# entity-framework entity-framework-core mysql


I have an MVC app with Entity Framework Core. It doesn't work when I run it (I could have swore it worked before), but it works about half the time when debugging. What did I do wrong with setting up my Models and whatnot? I followed a little step by step guide, but now I can't find it. Sorry if this is a duplicate, I couldn't find the right search keywords to find something similar.


public abstract class BaseProduct
    public int Id { get; set; }
    public string Brand { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Model { get; set; }
    [Display(Name = "Main Image")]

    public virtual FileDetails MainImage { get; set; }
    public virtual FileDetails AdditionalImages { get; set; }
    public virtual FileDetails Downloads { get; set; }



public class FileDetails
    public int Id { get; set; }
    public string FileName { get; set; }
    public string FileDir { get; set; }
    public string FileText { get; set; }



    public async Task<IActionResult> Index()
        var list = await _context.Compressor.ToListAsync();
        foreach (var compressor in list)
            if (compressor.MainImage == null)
                FileDetails img = new FileDetails(){FileDir = "", FileName =  "NoImage.png", FileText = ""};
                compressor.MainImage = img;
        return View(list);

So sometimes it does a join on the FileDetails table and sometimes it doesn't. The Compressor table (that inherits BaseProduct) has the IDs of the FileDetails table, so it seems like I didn't screw up too much.


Issue appears to be with Rider. Ran code in Visual Studio and is consistently working with it.

1/9/2018 9:45:33 PM

Popular Answer

I modified my code similar to Rainman's suggestion, but that still wasn't working.

var list = await _context.Compressor
    .Include(x => x.MainImage)
    .Include(x => x.AdditionalImages)
    .Include(x => x.Downloads).ToListAsync();

I ran the code with Visual Studio instead of Rider and it seems to be working pretty consistently.

1/9/2018 9:42:00 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow