即使禁用延迟加载,也会初始化非虚拟属性

时间:2015-06-27 14:28:15

标签: entity-framework-6 lazy-loading navigation-properties

我在我的Entity Framework数据库上下文中禁用了延迟加载。但是仍然存在以下模型的问题:

public class Currency
{
    //properties
    public ICollection<ConversionRate> ConversionRates{get;set;}

    public Currency()
    {
        ConversionRates = new HashSet<ConversionRate>();
    }
}

转换率等级如下:

public class ConversionRate
{
    [ForeignKey("FromCurrencyId")]
    public Currency FromCurrency { get; set; }

    public Guid FromCurrencyId { get; set; }

   [ForeignKey("ToCurrencyId")]
    public Currency ToCurrency { get; set; }

    public Guid ToCurrencyId { get; set; }
}

以下是配置:

///Currency
HasMany(x => x.ConversionRates).WithRequired(x => x.FromCurrency);
HasMany(x => x.ConversionRates).WithRequired(x => x.ToCurrency);

当我使用以下语句获取ConversionRate时:

var cr = _uow.ConversionRates.Query
           .Include(x=>x.FromCurrency)
           .Include(x=>x.ToCurrency)
           .FirstOrDefaultAsync(x=>x.Id==id);

填充以下集合:

cr.FromCurrency.ConversionRates
cr.ToCurrency.ConversionRates

我在填写馆藏时做错了什么?

1 个答案:

答案 0 :(得分:0)

您的ConversionRates集合需要定义为虚拟。 https://msdn.microsoft.com/en-us/data/jj574232.aspx#lazy