EntitySqlException未被用户代码处理?

时间:2012-08-01 05:06:51

标签: c#-4.0 entity-framework-4.1

运行我的c#代码时出现上述错误:

'CategoryID' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near simple identifier, line 6, column 1.

C#

 public ActionResult Index()
        {
            foreach (Category category in GetCategories("", "CategoryID", "", 1, 10, 1))
            {
                //do something
            }

            return View();
        }


        public IQueryable<Category> GetCategories(string filterExpression, string sortExpression, string sortDirection, int pageIndex, int pageSize, int pagesCount)
        {
            NorthwindEntities db = new NorthwindEntities();
            if (!String.IsNullOrWhiteSpace(filterExpression))
                return db.Categories.OrderBy(sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageSize);
            else
                return db.Categories.OrderBy(sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pagesCount * pageSize);
        }

1 个答案:

答案 0 :(得分:0)

使用ESQL时,必须使用实体别名来访问属性。默认别名为it,因此您必须使用"it.CategoryID"