Navigation properties not loading - eager Loading

eager-loading entity-framework-6

Accepted Answer

Your initialization of the reference navigation attribute is the primary problem.aDepartment = new Department(); (which causes confusion and inhibits the EF infrastructure from functioning correctly. Initializing collection navigation properties is acceptable (albeit not required), however reference navigation properties should never be initialised.

deleting the line above from theSubject Constructor will resolve the problem. Furthermore, even if it seems to function in your situation (surprise to me), it is preferable to adhere to naming rules and not refer to navigation property by that name.aDepartment , but justDepartment in order to align withDepartmentID FK real estate

The updated and functional version of theSubject 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

You may not have explained what the foreign keys are to EF, in my opinion. Where is the mapping class notifying EF about this connection, exactly? Please put [ForeignKey("DepartmentID")] attribute above the Department navigation property on Subject if you don't already have it and see if it addresses your problem, whether you have a 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