无法在mvc 4中获取特定类别的子类别并设计类别和子类别模型类的问题

时间:2014-08-26 05:41:02

标签: asp.net-mvc-4

我正在使用mvc中的帮助台系统。

我只有一个主表供用户和技术人员使用。

这是我的类别类:

 public class Category
    {
        [Key]
        public int CategoryId { get; set; }
        public string Name { get; set; }


        public virtual ICollection<SubCategory> subCategory { get; set; }//category can have more than 1 category
    }

这是我的子类别:

public class SubCategory
    {
          [Key]
          public int SubcategoryId { get; set; }
          public string Name { get; set; }
          public int CategoryId { get; set; }
          public virtual ICollection<TicketInfo> ticketsInfo { get; set; }/to keep track of all tickets under this particular subcategory.
          public virtual ICollection<UserDetails> technicianInfo { get; set; }//to keep track of technician and user under this subcategory.

          public virtual Category category { get; set; }
    }

这是我的用户主管(它定义了用户和技术人员)

public class UserDetails
    {
        public string UserName { get; set; }
        [Key]
        public int UserId { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }
        public string PhoneNo { get; set; }
        public string EmailId { get; set; }
        [DataType(DataType.Password)]
        public string Password { get; set; }

        public int SubcategoryId { get; set; }
        public int AddressId { get; set; }
        public Boolean IsActive { get; set; }
        public DateTime CreatedDate { get; set; }

        public virtual ICollection<Role> Roles { get; set; }

        public virtual SubCategory subCategory { get; set; }

    }

现在我正在解雇查询::

public list<Category> FetchTicketDetailsforSubcategory(int categoryId)
        {
            using (HelpDeskdbContext context = new HelpDeskdbContext())
            {
                var category = from temp in context.Category where                temp.CategoryId == categoryId select temp;

                return category;
            }   
        }

它只显示该类别,但不是该类别下的子类别。

它在子类别上显示了这个: ObjectContext实例已被处理,不能再用于需要连接的操作。

任何人都能弄明白我的班级设计有什么问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个

context.Category.Where(x=>x.CategoryId == categoryId).SelectMany(x=>x.subCategory).ToList()