3

Below are two classes that control database tables using Code First Entity Framework (DbContext).

public class Employee
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string EmployeeName { get; set; }

    public int DepartmentId { get; set; }

    public Department Department { get; set; }

}

--

public class Department
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string DepartmentName { get; set; }
}

If I retrieve the employees with the line below the navigation property for Department is null:

        var employees = _context.Employees.ToList();

However, if I first populate a separate variable with Departments, like this ...

        var departments = _context.Departments.ToList();

        var employees = _context.Employees.ToList();

... Each employee in the employees list contains the Department object.

My question: What is best practice to populate the navigation property? I had imagined that Entity Framework would have done this by default and that the ToList() method would handle the lazy loading.

4
  • 1
    If by notification property you mean navigation property, search for EF lazy loading, eager loading and explicit loading to learn which one is appropriate for you (there is no "best"). Commented Apr 2, 2017 at 18:49
  • 1
    Yes, I was referring to navigation property. I'll correct the question. As for lazy/eager loading: Shouldn't the ToList() method load the data? Commented Apr 2, 2017 at 18:51
  • 1
    It loads only that data, e.g. Employees, but not related data like Employee.Department. See msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx Commented Apr 2, 2017 at 18:57
  • Thank you. That's clear and solved my problem, I had looked for the Include() method but was missing using Microsoft.EntityFrameworkCore; Commented Apr 2, 2017 at 19:12

2 Answers 2

3

Reading the docs/roadmap you'd have realized that lazy loading is not yet supported in EF Core 1.0/1.1.

Currently only .Include or eager loading is supported, both well documented in the documentation.

You must not assume that all features from EF 6 are available in EF Core. EF Core is a complete rewrite and do not include many of the features of EF6. If you need any of that stuff, you should keep using EF6 instead.

EF Core is good enough for most simple/basic ORM stuff, but even Microsoft recommends to use EF6 for production where you depend on these features.

Sign up to request clarification or add additional context in comments.

Comments

2

I solved my problem in EF Core 2.1 using UseLazyLoadingProxies:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
        .UseLazyLoadingProxies();

You now need to manually specify that you want it enabled.

1 Comment

To be able to get that included, it's a separate Nuget package you'll have to pull down: Microsoft.EntityFrameworkCore.Proxies

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.