Entity Framework 4の場合プロキシクラスを使用せずに、いくつかのクエリをPOCOにロードすることを選択できますか?(将来の読み取り専用使用のためにそのオブジェクトをキャッシュする目的で)。リポジトリ-サービスパターンを使用しています。
これはつまり:
var order = _orderService.GetById(1);
// after order is loaded then we can see in the debugger that:
// order.Customer is of type System.Data.Entity.DynamicProxy.Customer_17631AJG_etc
私が欲しいのは、そのタイプのプロキシではなく、order.Customer
実際にPOCOタイプを使用することです。MyApp.Models.Entities.Customer
編集:「GetUnproxied」メソッドをリポジトリに追加するというLadislavの提案に基づいて、私はこの変更を行いました。
// this is the current method that must return a DynamicProxy
public IQueryable<T> GetQuery()
{
return ObjectSet.AsQueryable();
}
// this is the new additional method that must return the plain POCO
public IQueryable<T> GetReadOnly()
{
ObjectContext.ContextOptions.ProxyCreationEnabled = false;
var readOnly = ObjectSet.AsQueryable();
ObjectContext.ContextOptions.ProxyCreationEnabled = true;
return readOnly;
}
これは正しいです?
それは私にはスレッドセーフに見えません。どちらのメソッドも同じObjectContextインスタンスを使用するため、ProxyCreationEnabled == false
あるスレッドで発生してからpublic IQueryable<T> GetQuery()
別のスレッドで呼び出される可能性があります。これは、プロキシメソッドがプロキシされていないオブジェクトを返す可能性があることを突然意味します。