Navigation properties not loading - eager Loading

eager-loading entity-framework-6

Question

I have little experience with the Entity Framework. I am trying to use the Eager Loading but it seems that I am doing something wrong.

Although I am using the "include" in my query, the navigation property "Subjects" of the Department class, is not loading.

Below you can find the 2 POCO, DBContext and the Main method.

The output is just the title of the department ("Computer Science"), instead to display the related subjects ("Java","C#","C++").

public class Department
{
    public Department()
    {
        Subjects = new HashSet<Subject>();
    }

    public int ID { get; set; }
    public string Title { get; set; }

    public  ICollection<Subject> Subjects { get; set; }

}


 public class Subject
{
    public Subject()
    {
        aDepartment = new Department();
    }
    public int ID { get; set; }
    public string Title { get; set; }
    public int DepartmentID { get; set; }

    public  Department aDepartment { get; set; }        
}

   public class ModelDB : DbContext
{
    public ModelDB() : base("name=ModelDB")
    {
        Configuration.LazyLoadingEnabled = false;
       Configuration.ProxyCreationEnabled = true;
    }

    public DbSet<Department> Departments { get; set; }
    public DbSet<Subject> Subjects { get; set; }

}


static void Main(string[] args)
    {
        ModelDB db = new ModelDB();
        /*
        Department dep = new Department() { ID = 1, Title = "Computer Science" };
        db.Departments.Add(dep);
        db.SaveChanges();


        Subject s1 = new Subject() { ID=1, Title="Java", aDepartment = dep };
        db.Subjects.Add(s1);
        db.SaveChanges();

        Subject s2 = new Subject() { ID = 2, Title = "C#", aDepartment = dep };

        db.Subjects.Add(s2);
        db.SaveChanges();

        Subject s3 = new Subject() { ID = 3, Title = "C++", aDepartment=dep };
        db.Subjects.Add(s3);
        db.SaveChanges();
        */
        var lstDepartments = db.Departments.Include("Subjects");

        foreach (var d in lstDepartments)
        {

            Console.WriteLine(d.Title);
            var ss = d.Subjects;
            foreach(var s in ss)
            {
                Console.WriteLine(s.Title);
            }
        }

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();

    }
1
1
2/5/2018 6:47:48 PM

Accepted Answer

The main issue is that you are initializing the reference navigation property (aDepartment = new Department();) which is confusing the EF infrastructure and prevents it working properly. Initializing collection navigation properties is ok (although not mandatory), but never do that for reference navigation properties.

Removing the above line from the Subject constructor will fix the issue. Additionally, although it somehow works in your case (surprisingly to me), better follow the naming conventions and don't call navigation property aDepartment, but simply Department to line up with the DepartmentID FK property.

Here is the corrected working version of the Subject entity:

public class Subject
{
    public int ID { get; set; }
    public string Title { get; set; }
    public int DepartmentID { get; set; }
    public Department Department { get; set; }
}
3
2/5/2018 7:22:14 PM

Popular Answer

I feel like you might not have told EF what the foreign keys are. Where is the mapping class that is telling EF this link? If you don't have this, please just add [ForeignKey("DepartmentID")] attribute above the Department navigation property on Subject and see if that fixes your issue, new Department or not. :)



Related Questions





Related

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