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.

RAM
  • 2,257
  • 2
  • 19
  • 41
Sparked
  • 844
  • 10
  • 25
  • 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"). – Ivan Stoev Apr 02 '17 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? – Sparked Apr 02 '17 at 18:51
  • 1
    It loads only that data, e.g. `Employees`, but not **related** data like `Employee.Department`. See https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx – Ivan Stoev Apr 02 '17 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**; – Sparked Apr 02 '17 at 19:12

2 Answers2

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.

Tseng
  • 61,549
  • 15
  • 193
  • 205
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.

George Wurthmann
  • 411
  • 2
  • 8
  • 20
  • 4
    To be able to get that included, it's a separate Nuget package you'll have to pull down: Microsoft.EntityFrameworkCore.Proxies – Rob Koch Apr 23 '19 at 20:12