' System.NotSupportedException'发生在System.Data.Entity.dll中

时间:2015-09-15 07:19:10

标签: c# asp.net-mvc entity-framework asp.net-mvc-4

C#| .NET 4.5 |实体框架5

我以ID,RoleName,ParentId的形式从SQL查询返回数据。我想获取该数据并将其解析为Hierarchical JSON字符串。但是在获取记录时我收到了错误..

数据返回

        id   parentId    name
1       1      Null       ED
2       2      1          CPD
3       3      2          Centre Manager 
4       4      3          Manager
5       5      4          Sales Head
6       6      4          Technical Head
7       7      5          Sales Individual
8       8      6          Technical Individual

模型类

public partial class Role
{
    public int Id { get; set; }
    public string RoleName { get; set; }
    public Nullable<int> ParentID { get; set; }
}
public class RoleVM
{
    public string text { get; set; }
    public string icon { get; set; }
    public IEnumerable<RoleVM> node { get; set; }

}

代码

 public ActionResult getJsonTree()
    {
        var roles = getChilds(null).ToArray();
        return Json(roles, JsonRequestBehavior.AllowGet);
    }

   private IEnumerable<RoleVM> getChilds(int? parentID)
    {
        return _db.Roles
            .Where(r => r.ParentID == parentID)
            .Select(x =>
                    new RoleVM()
                    {
                        text=x.RoleName,
                        icon = "glyphicon glyphicon-user",
                        node=getChilds(x.Id)
                    }).ToList();
    }

错误

An exception of type 'System.NotSupportedException' occurred in 
System.Data.Entity.dll but was not handled in user code


Additional information: LINQ to Entities does not recognize the method   
'System.Collections.Generic.IEnumerable`1[SMS.Models.ViewModel.RoleVM] 
getChilds(System.Nullable`1[System.Int32])' method, and this method cannot
be translated into a store expression.

0 个答案:

没有答案