http://www.cnblogs.com/dabaopku/archive/2011/07/16/2108351.html
http://www.cnblogs.com/lizhao/archive/2012/07/24/2606888.html
http://msdn.microsoft.com/zh-cn/data/jj713564.aspx
Entity 在 MVC 行为上返回为 json 对象时要对实体进行属性导航。但是该Entity所在的Context已经在Controller中通过 using 释放掉;但是Entity又具有Deferred Query Evaluation性质,因此,导航属性对象没有被加载,从而出现上述错误。
解决方法一
关掉延迟加载LazyLoadingEnabled=false;
解决方法二 .ToList<T>()
public static List<Answer> GetAnswer_ByQuestionID(int questionID)
{
using (IA2SContext context = new IA2SContext())
{
var list = (from o in context.Answer
where o.QuestionID == questionID
select o).ToList<Answer>();
return list;
}
}
解决方法三
在查询中声明包含导航属性
public static List<Question> GetQuestionList_ByCategoryID(int categoryID)
{
using (IA2SContext context = new IA2SContext())
{
var list = (from o in context.Question.Include("Answer")
where o.CategoryID == categoryID && o.IsActive == true
select o).OrderBy(p => p.SortOrder).ToList<Question>();
return list;
}
}
}