gpt4 book ai didi

entity-framework-6 - 检查延迟加载属性已在 EF6 中加载

转载 作者:行者123 更新时间:2023-12-03 07:33:17 25 4
gpt4 key购买 nike

我在某些操作中通过反射使用类属性,因此在使用 DynamicProxy 实例时,它会导致加载整个数据库。 (700 多个类彼此相关)。

是否可以检查是否加载了延迟加载属性?禁用动态代理生成( ProxyCreationEnabled = false )在我的情况下不可用。

Customer oCustomer = context.get(1);

if(oCustomer.Location.HasLoaded)
do smt..

public class Customer
{
public decimal? Id {get; set;}
public virtual CustomerLocation Location{get; set;}
}

public class CustomerLocation
{
public decimal? Id {get; set;}
public string Detail {get; set;}
}

最佳答案

看起来您正在寻找 DbReferenceEntry<TEntity, TProperty>.IsLoadedDbReferenceEntry.IsLoaded属性(property):

if (context.Entry(oCustomer).Reference(e => e.Location).IsLoaded)

或者
if (context.Entry(oCustomer).Reference("Location").IsLoaded)

对于集合类型导航属性,只需使用 .Collection而不是 .Reference .

关于entity-framework-6 - 检查延迟加载属性已在 EF6 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37650689/

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