Guest User

Untitled

a guest
Jan 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. public class Category
  2. {
  3. public int ID { get; set; }
  4. public Category RootCategory { get; set; } // This one works good, it also creates "RootCategory_Id" in database on "update-database"
  5.  
  6. public ICollection<Category> ChildCategories { get; set; } // This is always null, how to map it correctly?
  7.  
  8. public string Name { get; set; }
  9. }
  10.  
  11. protected override void Seed(Test.Infrastructure.TestDataContext context)
  12. {
  13. context.Categories.Add(new Category() {
  14. Name = "First Category", ChildCategories = new List<Category>() {
  15. new Category(){
  16. Name = "Second Category"
  17. },
  18. new Category(){
  19. Name = "Third Category"
  20. }
  21. }
  22. });
  23.  
  24. context.SaveChanges();
  25. }
  26.  
  27. public ActionResult Test()
  28. {
  29. // After checking DB my root is 4, and two categories that have RootCategory_Id set to 4
  30. var c = _db.Categories.Where(x => x.ID == 4).Single();
  31. return Content(c.ChildCategories.FirstOrDefault().Name); // Always returns null, even c.ChildCategories.Count() returns 'null'
  32. }
  33.  
  34. _db.Categories.Include("ChildCategories").FirstOrDefault(x => x.ID == 4)
  35.  
  36. public class Category
  37. {
  38. public int ID { get; set; }
  39. public Category RootCategory { get; set; }
  40.  
  41. public IQueryable<Category> ChildCategories {
  42. get
  43. {
  44. TestDataContext _db = new TestDataContext();
  45. return _db.Categories.Where(x => x.RootCategory.ID == this.ID);
  46. }
  47. }
  48.  
  49. public string Name { get; set; }
  50. }
Add Comment
Please, Sign In to add comment