Lazy Loading does not work, related entity is always zero

I have a problem with EF. The implemented object is always zero. So far I have not had any solutions.

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, each category 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();

Most likely, it seems that evrything is okay, the associated object is always null, even with 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 associated object 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 objects where lazy loading works.

Only with these models (categories and subcategories) I have a problem. What am I doing wrong?

+4
4

SubCategories. , .

: public virtual Categories Categories { get; set; } . , EF , ( ) db, Include Lazy loading. CategoryID CategoriesID.B'cos 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; }
  }
+2

:

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; }
}
0

SubCategories:

public virtual Categories Categories { get; set; }
0

, , SubCategories, CategoryId CategoriesID.:) !

0

Source: https://habr.com/ru/post/1660756/


All Articles