Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework EntityReference.IsLoaded的EntityReference.Load检查是否已加载?_Entity Framework_Lazy Loading_Navigation Properties_Entityreference - Fatal编程技术网

Entity framework EntityReference.IsLoaded的EntityReference.Load检查是否已加载?

Entity framework EntityReference.IsLoaded的EntityReference.Load检查是否已加载?,entity-framework,lazy-loading,navigation-properties,entityreference,Entity Framework,Lazy Loading,Navigation Properties,Entityreference,嗨,我想知道这个方法是否包括 If Not ref.IsLoaded Then ref.Load() 我的问题基本上是: Dim person = Context.Persons.FirstOrDefault person.AddressReference.Load() person.AddressReference.Load() 'Does it do anything? 它确实再次加载。我通过Profiler验证了这一点,它显示了两个查询。默认的合并选项是MergeOption.Appe

嗨,我想知道这个方法是否包括

If Not ref.IsLoaded Then ref.Load()
我的问题基本上是:

Dim person = Context.Persons.FirstOrDefault
person.AddressReference.Load()
person.AddressReference.Load() 'Does it do anything?

它确实再次加载。我通过Profiler验证了这一点,它显示了两个查询。默认的合并选项是MergeOption.AppendOnly,它不会阻止再次查询。来自反射器的代码:

public override void Load(MergeOption mergeOption)
{
    base.CheckOwnerNull();
    ObjectQuery<TEntity> query = base.ValidateLoad<TEntity>(mergeOption, "EntityReference");
    base._suppressEvents = true;
    try
    {
        List<TEntity> collection = new List<TEntity>(RelatedEnd.GetResults<TEntity>(query));
        if (collection.Count > 1)
        {
            throw EntityUtil.MoreThanExpectedRelatedEntitiesFound();
        }
        if (collection.Count == 0)
        {
            if (base.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One)
            {
                throw EntityUtil.LessThanExpectedRelatedEntitiesFound();
            }
            if ((mergeOption == MergeOption.OverwriteChanges) || (mergeOption == MergeOption.PreserveChanges))
            {
                EntityKey entityKey = ObjectStateManager.FindKeyOnEntityWithRelationships(base.Owner);
                EntityUtil.CheckEntityKeyNull(entityKey);
                ObjectStateManager.RemoveRelationships(base.ObjectContext, mergeOption, (AssociationSet) base.RelationshipSet, entityKey, (AssociationEndMember) base.FromEndProperty);
            }
            base._isLoaded = true;
        }
        else
        {
            base.Merge<TEntity>(collection, mergeOption, true);
        }
    }
    finally
    {
        base._suppressEvents = false;
    }
    this.OnAssociationChanged(CollectionChangeAction.Refresh, null);
}
public覆盖无效加载(合并选项)
{
base.CheckOwnerNull();
ObjectQuery=base.ValidateLoad(合并选项,“EntityReference”);
base.\u suppressEvents=true;
尝试
{
列表集合=新列表(RelatedEnd.GetResults(查询));
如果(collection.Count>1)
{
抛出EntityUtil.More-ExpectedRelatedEntitiesFound();
}
if(collection.Count==0)
{
if(base.ToEndMember.RelationshipMultiplicity==RelationshipMultiplicity.One)
{
throw EntityUtil.lesststhanExpectedRelatedEntitiesFound();
}
if((mergeOption==mergeOption.OverwriteChanges)| |(mergeOption==mergeOption.PreserveChanges))
{
EntityKey EntityKey=ObjectStateManager.FindKeyOneTityWithRelationships(base.Owner);
EntityUtil.CheckEntityKeyNull(entityKey);
ObjectStateManager.RemoveRelationships(base.ObjectContext、mergeOption、(AssociationSet)base.RelationshipSet、entityKey、(AssociationEndMember)base.FromEndProperty);
}
基础。_isLoaded=true;
}
其他的
{
base.Merge(集合,mergeOption,true);
}
}
最后
{
base.\u suppressEvents=false;
}
this.OnAssociationChanged(CollectionChangeAction.Refresh,null);
}

这是我为当前项目创建的扩展方法,仅供其他找到公认答案的人参考

using System.Data.Objects.DataClasses;

namespace ProjectName
{
    public static class EntityFrameworkExtensions
    {
        public static void EnsureLoaded<TEntity>(this EntityReference<TEntity> reference)
            where TEntity : class, IEntityWithRelationships
        {
            if (!reference.IsLoaded)
                reference.Load();
        }
    }
}

谢谢你的努力,我真的很感激!我为RelatedEnd创建了一个扩展方法,可以在加载之前进行检查。有人对此有一个在EF6中有效的答案吗?
Patient patient = // get patient

patient.ClinicReference.EnsureLoaded();
patient.Clinic.DoStuff();