Entity Framework 6 Lazy Loading doesn't work

c# entity-framework entity-framework-6

Question

I have created two model classes: Student and Exam.

Student:

class Student
{
    [Key]
    public int StudentId { get; set; }

    [Required, MaxLength(30)]
    public string FirstName { get; set; }

    [Required, MaxLength(50)]
    public string LastName { get; set; }

    [Required]
    public DateTime BirthDate { get; set; }

    public virtual ICollection<Exam> Exams { get; set; } 
}

Exam:

public enum Grade
{
    A, B, C, D, E, F
}
class Exam
{
    [Key]
    public int ExamId { get; set; }

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

    [ForeignKey("Student"), Required]
    public int StudentId { get; set; }

    public virtual Student Student { get; set; }
}

But it seems Lazy Loading doesn't work, here's example how I retrieve data:

using (var context = new StudentContext())
{
    context.Configuration.LazyLoadingEnabled = true;
    context.Configuration.ProxyCreationEnabled = true;

    var student = context.Students.Single(s => s.LastName == "ABC");

    foreach (var exam in student.Exams)
    {
        Console.WriteLine($"{exam.ExamId}: {exam.Grade}");
    } 
    context.SaveChanges();
}

And I'm getting NullReferenceException when I try to access students.Exams property. What's wrong in my code?

The problem with my code is that Entity Framework Lazy Loading isn't working and I don't know why. It's not about not initializing Exams, because Entity Framework should do it alone when I try to access Exams property.

1
3
3/22/2016 11:11:12 PM

Accepted Answer

You didn't follow the rules to enable Entity framework to create proxies (required for lazy loading), which needs public access:

A custom data class must be declared with public access.

Make your Exam and Student public and it should work.

11
3/23/2016 7:50:40 AM

Popular Answer

In c# if you don't declare access modifiers, It will accept it as internal by default.

You should declare you entity classes as public.

Secondly collections should return empty collection beside of null. So If a student doesn't have any exam, you should return empty collection. That's wyh you should change your Student Constructor also.

 public class Student
    {
        public Student()
        {
            Exams = new List<Exam>();
        }
    }

Edit: If you have ICollection or IList, ef returns empty collection. You should use this constructor, if you are using IEnumurable.



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