How to create setter in root-child category example in EF

cobain325

New Member
I have very simple model for categories. Category can have child categories and root category. Problem is when I try to set the root category, for some reason it won't set. My get method works good, but not so much for set.Here is my model\[code\]public class Category{ public int ID { get; set; } public Category RootCategory { get { ORDataContext _db = new ORDataContext(); return _db.Categories.Where(x => x.ChildCategories.Any(t => t.ID == this.ID)).SingleOrDefault(); } set { } } public ICollection<Category> ChildCategories { get { ORDataContext _db = new ORDataContext(); return _db.Categories.Where(x => x.RootCategory.ID == this.ID).ToList(); } } public string Name { get; set; }}\[/code\]And this is my create action\[code\][HttpPost]public ActionResult Create(Category c, int? rootCategoryID){ var rootCategory = _db.Categories.Where(x => x.ID == rootCategoryID).SingleOrDefault(); c.RootCategory = rootCategory; // Also tried c.RootCategory = new Category() { ID = rootCategory.ID }; return Content(Infrastructure.Helpers.SerializeObject(c)); // c.RootCategory = null all the time}\[/code\]
 
Back
Top