gpt4 book ai didi

entity-framework - Entity Framework 急于加载/"IncludeEverything()"?

转载 作者:行者123 更新时间:2023-12-04 18:11:46 26 4
gpt4 key购买 nike

在 EF4 中,我的对象图很小,数据量也很小。因此,对于查询,我想急切地加载所有相关数据。是否有任何单一的方法调用可以完成这项工作,例如 "IQueryable.IncludeEverything()" ,而不是调用 Include()反复使用硬编码的属性名称?

最佳答案

没有什么是开箱即用的,但您可以使用 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;
}

请注意,此代码仅用于说明。它没有参数验证,它可能多次包含相同的实体,并且如果您的模型中有循环,它不会包含所有相关实体。

关于entity-framework - Entity Framework 急于加载/"IncludeEverything()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12486741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com