EF4では、小さなオブジェクトマップがあり、データの量も少ないです。したがって、クエリの場合は、関連するすべてのデータを熱心にロードします。ハードコードされたプロパティ名で繰り返し"IQueryable.IncludeEverything()"
呼び出すのではなく、のようにジョブを実行できる単一のメソッド呼び出しはありますか?Include()
1 に答える
4
箱から出してすぐに使えるものはありませんが、MetadataWorkspaceを使用して実装できます。
public static IQueryable<T> IncludeEverything<T>(this IQueryable<T> query, ObjectContext context)
where T : class
{
var ospaceEntityType = context.MetadataWorkspace.GetItem<EntityType>(
typeof(T).FullName, DataSpace.OSpace);
var cspaceEntityType = context.MetadataWorkspace.GetEdmSpaceType(ospaceEntityType);
var includedTypes = new HashSet<EdmType>();
includedTypes.Add(cspaceEntityType);
return IncludeEverything(query, cspaceEntityType as EntityType, "", includedTypes);
}
private static IQueryable<T> IncludeEverything<T>(IQueryable<T> query,
EntityType entity,
string path,
HashSet<EdmType> includedTypes)
where T : class
{
foreach (var navigationProperty in entity.NavigationProperties)
{
var propertyEdmType = navigationProperty.TypeUsage.EdmType;
if (includedTypes.Contains(propertyEdmType))
{
continue;
}
includedTypes.Add(propertyEdmType);
var propertyCollectionType = propertyEdmType as CollectionType;
EntityType propertyEntityType;
if (propertyCollectionType != null)
{
propertyEntityType = propertyCollectionType.TypeUsage.EdmType as EntityType;
} else
{
propertyEntityType = propertyEdmType as EntityType;
}
var propertyPath = string.IsNullOrEmpty(path) ? "" : path + ".";
propertyPath += navigationProperty.Name;
query = query.Include(propertyPath);
query = IncludeEverything(query, propertyEntityType, propertyPath, includedTypes);
}
return query;
}
このコードは説明のためだけのものであることに注意してください。パラメータ検証がなく、同じエンティティが数回含まれている可能性があり、モデルにサイクルがある場合、関連するすべてのエンティティが含まれるわけではありません。
于 2012-09-19T19:07:47.543 に答える