私は以下のようなデータベースを持っています:
私はEFを使用していて、オブジェクトを取得しようとしています。通常、データベースからPRODUCTエンティティを選択した後、以下のようにPRODUCTエンティティのACCESSLEVELエンティティに到達できます。
選択では、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 で製品を取得することです。
どうやってやるの?
前もって感謝します。