milesblush
New Member
All I want to create is basic recursive category. Category is root if \[code\]RootCategory_Id\[/code\] is set to null and it belongs to some other category if it is set to some id. I've added category with two child-categories in \[code\]Seed()\[/code\] method to test and it does not work. (I've checked DB afterwards, there are inserted)Category model\[code\]public class Category{ public int ID { get; set; } public Category RootCategory { get; set; } // This one works good, it also creates "RootCategory_Id" in database on "update-database" public ICollection<Category> ChildCategories { get; set; } // This is always null, how to map it correctly? public string Name { get; set; }}\[/code\]Seed method\[code\]protected override void Seed(Test.Infrastructure.TestDataContext context){ context.Categories.Add(new Category() { Name = "First Category", ChildCategories = new List<Category>() { new Category(){ Name = "Second Category" }, new Category(){ Name = "Third Category" } } }); context.SaveChanges();}\[/code\]This is how I tested that it does not work\[code\]public ActionResult Test(){ // After checking DB my root is 4, and two categories that have RootCategory_Id set to 4 var c = _db.Categories.Where(x => x.ID == 4).Single(); return Content(c.ChildCategories.FirstOrDefault().Name); // Always returns null, even c.ChildCategories.Count() returns 'null'}\[/code\]Picture of what I want to achievethis was generated from database-first approach using linq-to-sql
