Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 275
Orsi Avatar asked Oct 17 '22 22:10

Orsi


1 Answers

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; }
  }
like image 170
Sampath Avatar answered Oct 21 '22 02:10

Sampath