エンティティフレームワーク4.1で小さな質問があります。
画像DTOのインターフェースがあります:
public interface IImage : IDtoBase
{
string FullFilePath { get; set; }
int Width { get; set; }
int Heigth { get; set; }
long ImageTypeId { get; set; }
IImageType Type { get; set; }
}
コンテキストを構成するコードがあります:
// where TContext : DbContext, new()
private TInterface Invoke(Func<TContext, TInterface> callback)
{
using (var context = new TContext())
{
context.Configuration.AutoDetectChangesEnabled = true;
context.Configuration.LazyLoadingEnabled = true;
context.Configuration.ProxyCreationEnabled = true;
context.Configuration.ValidateOnSaveEnabled = true;
context.Database.Connection.Open();
return callback.Invoke(context);
}
}
必要なDTOアイテムを取得するためのコードがあります:
public TInterface Get(long id)
{
return Invoke(
context =>
{
TDto dto = context.Set<TDto>().FirstOrDefault(x => (x.Id == id));
return dto.Convert<TDto, TInterface>();
}
);
}
を設定context.Configuration.LazyLoadingEnabled = false
すると、画像DTOの「Type」プロパティはnullになります(大丈夫だと思います)。
が「true」の値の場合context.Configuration.LazyLoadingEnabled
、画像DTOの「Type」プロパティは「using」ステートメント内で正しい値を持ちますが、このプロパティはコンテキストの破棄後に破棄されます-「ObjectContextインスタンスは破棄され、操作に使用できなくなりました接続が必要です。」
つまり、イメージDTOは存在する/破棄されますが、その「Type」プロパティはすでに破棄されています。
誰かが解決策を提供できますか?「Type」プロパティを破棄しないでください(「Dispose」パターンの代わりに「using」ステートメントを使用したい)?