当前位置: 首页 > 编程语言 > C#

EntityFramework-eagerloadinginsteadofinclusion-分享

时间:2023-04-10 14:48:43 C#

EntityFramework:急切加载而不是包含?我的数据模型有很多嵌套实体,我想急切地加载整个对象树......除了将按需显式加载的视图实体。使用包含路径我必须指定很多路径,每次添加新实体时,我都必须调整这些包含路径。我目前使用我的存储库的以下方法来加载一个类型的所有实体:string[]includePaths=commaSeperatedIncludePropertyPaths.Split(new[]{','},StringSplitOptions.RemoveEmptyEntries);返回includePaths.Aggregate(initialQuery,(currentQuery,includeProperty)=>currentQuery.Include(includeProperty));传递的包含路径已经填满了整个屏幕。所以,我希望EntityFramework能够自动加载所有导航属性,除了那些我用排除路径指定的属性:publicvirtualIQueryableAll(stringcommaSeperatedExcludePropertyPaths=""){//...如何实现?排除路径有助于避免循环依赖,并过滤掉我不想急切加载的少数实体。指定排除而不是包含减少样板代码。这可能是EF6.1.3或EF7的计划吗?如果不是,选择的原因是什么?是否有人已经尝试读取实体元数据并将其应用于“自动预加载”并失败了?相关(旧)问题和文章:https://msdn.microsoft.com/en-us/magazine/hh205756.aspx实体框架自动加载实体框架-有没有办法在没有Include()的情况下自动加载子实体?entityframeworklinqqueryInclude()multiplesubentitiesentityframework.Include()withcompiletimechecking?以下是解决方案的初稿。我仍然需要弄清楚它是否实用......我还会考虑重新加载加载方法(如Lanorkin建议的那样)。谢谢您的意见。编辑事实证明,虽然在开发应用程序时排除可能有意义......对域模型进行了许多更改......排除并不比我刚刚考虑的“真实世界示例”包含更优雅。a)我浏览了我的实体并计算了包含和排除的导航属性的数量。排除属性的平均数量并不比包含属性的数量小很多。b)如果我确实考虑了排除的不同导航属性“foos”,我将被迫考虑排除Foo类型的子实体......如果我根本不想使用它的属性。另一方面,使用contains,我只需要为子实体指定导航属性“foos”即可。因此,虽然排除可能会在一个级别保留一些规范,但它们需要更多规范才能进入下一个级别......(当排除一些中间实体而不仅仅是位于加载对象树的叶子上的实体时)。c)此外,包含/排除可能不仅取决于实体的类型,还取决于用于访问实体的路径。然后需要指定排除项,例如“为一个目的加载实体时排除属性xy,为另一个目的加载实体时排除属性z”。=>由于这些考虑,我将继续使用include。我根据包含字典的类型而不是字符串实现了保存包含:privatestaticreadonlyInclusions_personInclusionsWithCompanyParent=newInclusions(typeof(Company)){{e=>e.Company,false},{e=>e.Roles,true}};我有一个从包含列表创建查询的方法。该方法还检查字典中是否考虑了所有现有的导航属性。如果我添加一个新实体而忘记指定相应的包含,则会抛出异常。不过,这里有一个实验性的解决方案,使用excludes而不是include:如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——privateconstintMAX_EXPANSION_DEPTH=10;私人DbContext上下文{得到;放;}//在构建我的存储库时设置publicvirtualIQueryableAllExcluding(stringexcludeProperties=""){varpropertiesToExclude=excludeProperties.Split(new[]{','},StringSplitOptions.RemoveEmptyEntries);IQueryableinitialQuery=Context.Set();varelementType=initialQuery.EvarelementType=initialQuery。navigationPropertyPaths=newHashSet();varnavigationPropertyNames=GetNavigationPropertyNames(elementType);foreach(varpropertyNameinnavigationPropertyNames){if(!propertiesToExclude.Contains(propertyName)){ExtendNavigationPropertyPaths(navigationPropertyPaths,elementType,propertyName,propertyName,propertiesToExclude,0);}}返回navigationPropertyPaths.Aggregate(initialQuery,(current,includeProperty)=>current.Include(include财产));}privatevoidExtendNavigationPropertyPaths(ISetnavigationPropertyPaths,TypeparentType,stringpropertyName,stringpropertyPath,ICollectionpropertiesToExclude,intexpansionDepth){if(expansionDepth>MAX_EXPANSION_DEPTH){返回;}varpropertyInfo=parentType.GetProperty(propertyName);varpropertyType=propertyInfo.PropertyType;varisEnumerable=typeof(IEnumerable).IsAssignableFrom(propertyType);如果(isEnumerable){propertyType=propertyType.GenericTypeArguments[0];}varsubNavigationPropertyNames=GetNavigationPropertyNames(propertyType);varnoSubNavigationPropertiesExist=!subNavigationPropertyNames.Any();如果(noSubNavigationPropertiesExist){navigationPropertyPaths.Add(propertyPath);返回;}foreach(varsubPropertyNameinsubNavigationPropertyNames){if(propertiesToExclude.Contains(subPropertyName)){navigationPropertyPaths.Add(propertyPath);继续;}varsubPropertyPath=propertyPath+'.'+副公关物业名称;ExtendNavigationPropertyPaths(navigationPropertyPaths,propertyType,subPropertyName,subPropertyPath,propertiesToExclude,expansionDepth+1);}}privateICollectionGetNavigationPropertyNames(TypeelementType){varobjectContext=((IObjectContextAdapter)Context).ObjectContext;varentityContainer=objectContext.MetadataWorkspace.GetEntityContainer(objectContext.DefaultContainerName,DataSpace.CSpace);varentitySet=entityContainer.EntitySets.FirstOrDefault(item=>item.ElementType.Name.Equals(elementType.Name));if(entitySet==null){returnnewList();}varentityType=entitySet.ElementType;返回entityType.NavigationProperties.Select(np=>np.Name).ToList();}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员。注明出处: