Orsi
Orsi

Reputation: 575

Lazy Loading not working, related entity is always null

I Have a problem with the EF. The realated entity is always null. I didn't get any solutions so far.

Here are the models:

 public class Categories
 {
    public int ID { get; set; }
    public string Name { get; set; }
    public int AtpID { get; set; }

    public virtual ICollection<SubCategories> SubCategories { get; set; }
 }

 public class SubCategories
 {
    public int ID { get; set; }
    public string Name { get; set; }
    public int CategoryID { get; set; }
    public string LinkToProducts { get; set; }
  }

So, every Categorie has more subcategories. In the Seed method I populated the database, so I have data:

var categories = new List<Categories>
        {
        new Categories{Name="Abgasanlage", ID=1},
        new Categories{Name="Elektrik",ID=2},
        new Categories{Name="Filter", ID=3},
        new Categories{Name="Karosserie", ID=4},
        new Categories{Name="Kuhlunkg",ID=5}
        };

        categories.ForEach(s => context.Categories.Add(s));
        context.SaveChanges();

        var subCategories = new List<SubCategories>
        {
        new SubCategories{Name="Montageteile", ID=1, CategoryID=1},
        new SubCategories{Name="Lamdasonde",ID=2, CategoryID=1},
        new SubCategories{Name="Anlasser", ID=3, CategoryID=2},
        new SubCategories{Name="Luftfilter", ID=4, CategoryID = 3},
        new SubCategories{Name="Ohlfilter", ID=5, CategoryID = 3},
        new SubCategories{Name="Sonstige", ID=6, CategoryID = 4},
        new SubCategories{Name="Wasserpumpe", ID=7, CategoryID = 5}
        };

        subCategories.ForEach(s => context.SubCategories.Add(s));
        context.SaveChanges();

Altought it seems that evrything is ok, the related entity is always null, even with the Include(), is null.

I tried this way:

Models.Categories entity = db.Categories.Where(m => m.ID == 3)
                                   .Include(m => m.SubCategories)
                                   .FirstOrDefault();

but entity.SubCategories is always null.

with Include also the related entity is null

var setting = (from s in db.Categories.Include("SubCategories")
                           where s.ID == 3
                           select s).FirstOrDefault();

enter image description here

In my project I have more related entities, where the lazy loading is working.

Only with these models (Categories and SubCategories) I have the problem. What I'm doing wrong?

Upvotes: 5

Views: 3651

Answers (3)

Sampath
Sampath

Reputation: 65870

Your problem is on your SubCategories model.You have to fix that as shown below.

Note : use public virtual Categories Categories { get; set; } on it.Hence you didn't do that,EF doesn't know how to fetch the related entities (or navigational property) from the db when you use Include or Lazy loading.And also you need to change CategoryID as CategoriesID.B'cos your model's name is Categories.

public class SubCategories
 {
    public int ID { get; set; }
    public string Name { get; set; }

    [ForeignKey("CategoriesID")]
    public virtual Categories Categories{ get; set; }//you have to do this
    public int CategoriesID { get; set; }

    public string LinkToProducts { get; set; }
  }

Upvotes: 3

Fourat
Fourat

Reputation: 2447

You are missing the navigation property in SubCategories class:

public virtual Categories Categories { get; set; }

Upvotes: 0

Marcus
Marcus

Reputation: 8659

Try this:

public class SubCategories
{
    public int ID { get; set; }
    public string Name { get; set; }
    [ForeignKey("CategoryID")]
    public Categories Category { get; set; }
    public int CategoryID { get; set; }
    public string LinkToProducts { get; set; }
}

Upvotes: 0

Related Questions