Asp.net mvc 5 如何取消实体框架6的延迟加载 公共类国家/地区:项目 { 公共字符串代码 {get;set;} 公共字符串电话机 {get;set;} 公共字符串名 {get;set;} 公共字符串标志 {get;set;} 公共十进制?纬度 {get;set;} 公共十进制经度 {get;set;} 公共区域计数 {get;set;} [外汇(“默认货币”)] public int?DefaultCurrencyID {get;set;} 公共虚拟货币默认货币 {get;set;} 公共大陆型大陆型 {get;set;} 公共虚拟ICollection 性质 {get;set;} 公共ICollection 乡村地区 {get;set;} } 公共类CountryLocale:ItemLocale { [外键(“国家”)] 公共国际国家ID {get;set;} 公共国家 国 {get;set;} 公共字符串全名 {get;set;} } public TEntity Get(表达式,其中,参数字符串[]包含) { var模型=this.DbSet; foreach(includes中的var属性) { model.AsExpandable().Include(属性); } 返回模型.Where(Where).FirstOrDefault(); } this.Configuration.LazyLoadingEnabled=false; this.Configuration.ProxyCreationEnabled=false; Country Country=this.\u CountryRepository.Get(p=>p.ID==this.CountryID,新字符串[]{“CountryLocales”});
值不能为null。 参数名称:源 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:System.ArgumentNullException:值不能为null。 参数名称:源 行:CountryLocale countrylo=country.CountryLocales.First()Asp.net mvc 5 如何取消实体框架6的延迟加载 公共类国家/地区:项目 { 公共字符串代码 {get;set;} 公共字符串电话机 {get;set;} 公共字符串名 {get;set;} 公共字符串标志 {get;set;} 公共十进制?纬度 {get;set;} 公共十进制经度 {get;set;} 公共区域计数 {get;set;} [外汇(“默认货币”)] public int?DefaultCurrencyID {get;set;} 公共虚拟货币默认货币 {get;set;} 公共大陆型大陆型 {get;set;} 公共虚拟ICollection 性质 {get;set;} 公共ICollection 乡村地区 {get;set;} } 公共类CountryLocale:ItemLocale { [外键(“国家”)] 公共国际国家ID {get;set;} 公共国家 国 {get;set;} 公共字符串全名 {get;set;} } public TEntity Get(表达式,其中,参数字符串[]包含) { var模型=this.DbSet; foreach(includes中的var属性) { model.AsExpandable().Include(属性); } 返回模型.Where(Where).FirstOrDefault(); } this.Configuration.LazyLoadingEnabled=false; this.Configuration.ProxyCreationEnabled=false; Country Country=this.\u CountryRepository.Get(p=>p.ID==this.CountryID,新字符串[]{“CountryLocales”});,asp.net-mvc-5,lazy-loading,entity-framework-6,Asp.net Mvc 5,Lazy Loading,Entity Framework 6,值不能为null。 参数名称:源 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:System.ArgumentNullException:值不能为null。 参数名称:源 行:CountryLocale countrylo=country.CountryLocales.First() 由于性能原因,我在尝试取消激活懒散加载时出错。我怎样才能解决这个问题?提前感谢。当您禁用了LazyLoading时,您需要使用Includ
由于性能原因,我在尝试取消激活懒散加载时出错。我怎样才能解决这个问题?提前感谢。当您禁用了
LazyLoading
时,您需要使用Include
手动加载主对象的子属性
public class Country : Item
{
public string Code
{get; set;}
public string CodePhone
{ get; set; }
public string Name
{get; set;}
public string Flag
{ get; set; }
public decimal? Latitude
{get; set;}
public decimal? Longitude
{get; set;}
public int RegionsCount
{get; set;}
[ForeignKey("DefaultCurrency")]
public int? DefaultCurrencyID
{get; set;}
public virtual Currency DefaultCurrency
{ get; set; }
public ContinentType ContinentType
{ get; set; }
public virtual ICollection<Property>
Properties
{get; set;}
public ICollection<CountryLocale>
CountryLocales
{ get; set; }
}
public class CountryLocale : ItemLocale
{
[ForeignKey("Country")]
public int CountryID
{get; set;}
public Country
Country
{get; set;}
public string FullName
{get;set;}
}
public TEntity Get(Expression<Func<TEntity, bool>> where, params string[] includes)
{
var model = this.DbSet;
foreach (var property in includes)
{
model.AsExpandable().Include(property);
}
return model.Where(where).FirstOrDefault();
}
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
Country country = this._CountryRepository.Get(p=>p.ID == this.CountryID, new string[] { "CountryLocales" });
另一种方法是:
- 不要禁用延迟加载并在查询中使用
include
- 尝试一个接一个地包含您的子对象,直到您感觉到 性能更好
- 通常,如果您知道您将在 在查询该子对象时应使用的子对象 反对
- 如果您需要访问第二级的属性,请记住包含第二级子级<代码>包括(“父.子”)
var country = db.Country.Include("CountryLocales");