EF6 Linq Navigation properties are always null

Jeff Vanhorn 21 Reputation points

Here is the setup

I have a product table

public partial class Product
public uint Id {get; set;}
public uint TypeId {get; set;}
public virtual ProductType ProductTypeNavigation { get; set; } = null!;

public partial class ProductType
public uint Id {get; set;}
public string? Caption { get; set; }

context for _db
public virtual DbSet<Product> Products { get; set; } = null!;

linq query
Product product = _db.Products.FirstOrDefault(x=>x.Id = 1);

What do I need to do in order for ProductTypeNavigation to be populated? It always returns null. I can see product.TypeId is populated correctly. Tried using .first instead of firstordefault still null. Tried .Where().ToList().First() same result. Also tried installing lazyload proxies still null.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
626 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 24,676 Reputation points

    Include the ProductTypeNavigation property in the LINQ expression.

    var data = context.Products  
        .Include(p => p.ProductTypeNavigation)  

    Reference documentation
    Loading Related Data
    Eager Loading of Related Data

0 additional answers

Sort by: Most helpful