データ アクセス オブジェクトと、ループから呼び出されるData
署名付きのこのデータ アクセス オブジェクトのメソッドが与えられた場合、ループからの呼び出しごとに EF データ コンテキストを作成するのではなく、EF データ コンテキストを再利用する最善の方法は何ですか?public void GetEntityAttributesValues(int sessId, int entId)
foreach
ループ:
foreach (var ord in Data.Entities.Where(m => m.SessionId == CurrentSessionId))
{
Data.GetEntityAttributesValues(sid, ord.Id);
...
}
方法:
public void GetEntityAttributesValues(int sessId, int entId)
{
var tsOptions = new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted };
using (var scope = new TransactionScope(TransactionScopeOption.Required, tsOptions))
{
using (var context = new MyDataEntities(MyDataConnection))
{
var attVals = context.OrderAttributeValues.Where(a => a.SessionId == sessId
&& a.OrderId == entId).ToList();
foreach (var attVal in attVals)
{
var att = Attributes.Single(a => a.Key == attVal.AttributeId);
AttributeValues[att.Value] = attVal.AttributeValue;
}
scope.Complete();
}
}
}
したがって、このメソッドがループから呼び出されるたびに using ブロック内に新しいコンテキストを作成する代わりに、データ コンテキストを再利用したいと思います...