Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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; }
- }
- 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();
- }
- 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'
- }
- _db.Categories.Include("ChildCategories").FirstOrDefault(x => x.ID == 4)
- public class Category
- {
- public int ID { get; set; }
- public Category RootCategory { get; set; }
- public IQueryable<Category> ChildCategories {
- get
- {
- TestDataContext _db = new TestDataContext();
- return _db.Categories.Where(x => x.RootCategory.ID == this.ID);
- }
- }
- public string Name { get; set; }
- }
Add Comment
Please, Sign In to add comment