集合未加载

时间:2016-12-12 08:59:37

标签: c# entity-framework asp.net-core entity-framework-6 entity-framework-core

这些是我的课程

public class Foo
{
    //private ICollection<Bar> _bars; tried both ways with commented and uncommented fragments

    public int Id { get; set; }

    public string Value { get; set; }

    public virtual ICollection<Bar> Bars { get; set; }
        //get { return _bars ?? (_bars = new List<Bar>()); }
        //protected set { _bars = value; }


    public DateTime CreatedOnUtc { get; set; }
}

public class Bar
{
    public int Id { get; set; }

    [ForeignKey("Foo")]
    public int FooId { get; set; }
    public virtual Foo Foo { get; set; }

    public string Value { get; set; }
}

// in my DbContext I have
DbSet<Foo> Foos { get; set; }

当我用

加载元素集时
    public IQueryable<Core.Domain.Foo.Foo> GetFoos(bool include)
    {
        return include ? _fooRepository.Table.Include(x => x.Bars).AsTracking() : _fooRepository.Table;
    }

Bar个实体已加载。但是当我尝试加载单个实体_fooRepository.Table.FirstOrDefault(x => x.id == fooId)时,我无法加载Bar集合,它总是为空或为null,具体取决于Foo类定义中的注释或取消注释。

如何使这个工作?

0 个答案:

没有答案