我有一个数据库如下:
我正在使用 EF 并试图获取对象。在正常情况下,从数据库中选择PRODUCT Entity 后,我可以达到PRODUCT Entity 的ACCESSLEVEL Entity,如下所示:
在选择时,我使用INCLUDE(注意:下面的代码可以正常工作!)
//In a method located in some BL layer
public ProductCollection Load()
{
ProductCollection Result = new ProductCollection(); //Derived from List
using (var Context = base.Entities)
{
collection = from ItemProduct in Context.Product
.Include("AccessLevel");
return Result.AddRange(collection);
}
}
//In page load in aspx.cs file
foreach(var product in BL.Load())
{
Response.Write(product.AccessLevel.Name);
}
但是,在这里,当我执行以下操作时,它不起作用!
//In a method located in some BL layer
public ProductCollection Load()
{
ProductCollection Result = new ProductCollection(); //Derived from List
using (var Context = base.Entities)
{
collection = from ItemProduct in Context.Product
.Include("AccessLevel")
.Join(Context.Product_Category_Map.Where(c => c.ProductCategoryId == 3),
product => product,
map => map.Product,
(product, map) => product
));
;
return Result.AddRange(collection);
}
}
//In page load in aspx.cs file
foreach(var product in BL.Load())
{
//I Get Exception here and cannot react the included object
Response.Write(product.AccessLevel.Name);
}
例外是:
ObjectContext 实例已被释放,不能再用于需要连接的操作。
我最终想要的是通过给定的 ProductCategory Id 获取产品。
我怎样才能做到这一点?
提前致谢。