Click here to Skip to main content
15,889,216 members
Articles / Programming Languages / C#
Tip/Trick

Entity Framework Code First Navigation Property is null - solution

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Mar 2013CPOL1 min read 30.3K   2   3
A simple possible solution to Entity Framework Code First Navigation Property/Related Records not being loaded automatically

Introduction 

If you are using Code First Approach to Entity Framework and you use Visual Studio Snippets support ("class" snippet)  to construct your entities POCO classes, you might sometimes notice that your Navigation properties are not loaded at run time even if you mark the navigation properties as virtual. A simple workaround might solve your issue. Just add the "public" modifier to your class definition, which Visual Studio by default does not add.

Background

I am a great fan of Visual Studio snippets. When I was first using Code First development, I bumped into the strange behavior of my application where my navigation properties would not load even if everything looked right at the database side (relationships, foreign keys, etc.)  I toggled lazy loading, etc., without help. Finally, it was just leaving out an access modifier "public" causing these issues.

Using the code

In the following code, the classes are prefixed with public, which might be missing in the default VS snippet. Without this modifier, if you try to access the related record (in this case either Designation or Employees), you will find that it is null. Even if you enable lazy loading and mark the navigation properties as virtual, it will not work. In that case, add "public" keyword as shown in the following example.

C#
//employees table
public class Employee
{
    [Key]
    public int EmpId { get; set; }

    [Required, MaxLength(100)]
    public string EmployeeName { get; set; }

    [Required]
    public Designation Designation { get; set; }

}

//Designations table.
public class Designation
{
    [Key]
    public int DesigId { get; set; }

    [Required, MaxLength(100)]
    public string DesignationName { get; set; }

    
    public virtual ICollection<Employee> Employees { get; set; }  

}

//example of accessing the code from a different class:
MyDBContext context = new MyDBContext();
var desig = context.Employees.FirstOrDefault().Designation.DesignationName;

Points of Interest

It is interesting that adding/leaving out an access modifier like "public" or "virtual" can have a great impact on how the entire application is going to work and can lead to a developer losing his valuable time for no apparent reason.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Nejimon CR started coding in early 2000s with classic Visual Basic 6 and later moved to .NET platform. His primary technology stack expertise is around Microsoft technologies, but with the previous and latest areas of functioning to include a wide variety of technologies such as Win32 APIs, AutoIt scripting, UI Automation, ASP.NET MVC and Web API, Node.js, NoSQL, Linq, Entity Framework, AngularJS, etc.

His Articles on CodeProject:
http://www.codeproject.com/Articles/1060520/Centralizing-WCF-Client-Configuration-in-a-Class-L
http://www.codeproject.com/Articles/567356/Asynchronous-Access-of-Web-Service-from-WPF-with-B
http://www.codeproject.com/Articles/63849/Serial-Foot-Pedal-Device-Server
http://www.codeproject.com/Tips/149249/Simplest-way-to-implement-irregular-forms-in-NET
http://www.codeproject.com/Tips/564388/Entity-Framework-Code-First-Navigation-Property-is

Comments and Discussions

 
QuestionIt help me much, thank you! Pin
zzy_04711-Feb-18 15:12
zzy_04711-Feb-18 15:12 
QuestionNice! Pin
farid fereidoony11-Sep-14 9:45
farid fereidoony11-Sep-14 9:45 
AnswerRe: Nice! Pin
Nejimon CR23-Sep-14 1:39
Nejimon CR23-Sep-14 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.