1
我正在尝试为实体框架创建一个通用Get方法,其中动态为Where
和Include
。我使用下面的代码,但是当我尝试访问包含列表中的导航属性时,出现关于对象上下文被关闭的错误ObjectContext使用Include时发生封闭错误?
是不是.Include()
应该加载这些对象,所以我不会是否需要保持ObjectContext的打开?
public static List<T> GetList<T>(Func<T, bool> where, string[] includes)
where T : EntityObject
{
using (var context = new TContext())
{
ObjectQuery<T> q = context.CreateObjectSet<T>();
foreach (string navProperty in includes)
{
q.Include(navProperty);
}
return q.Where<T>(where).ToList();
}
}
代码导致错误:
var x = DAL<MyContext>.GetList<MyEntity>(
p => p.Id == 1
, new string[]{ "Type" });
var y = x.Type; // Throws an error that context has been closed
我觉得我必须做出某种愚蠢的错误在这里,因为我是新来的EF和我仍然在试图弄明白。
哦...><我知道这会是一件愚蠢的事情,谢谢 – Rachel