1

I'm using EF Core 2.1 and I have these class in my Domain.

public class HomeSection2
{
    public HomeSection2()
    {
        HomeSection2Detail = new List<HomeSection2Detail>();
    }

    public Guid ID { get; set; }
    public string Title { get; set; }
    public string Header { get; set; }

    public List<HomeSection2Detail> HomeSection2Detail { get; set; }
}

public class HomeSection2Detail
{
    public Guid ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Link { get; set; }
    public int? Sequence { get; set; }

    public HomeSection2 HomeSection2 { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.RemovePluralizingTableNameConvention();

    //HomeSection2
    modelBuilder.Entity<HomeSection2>().HasKey(s => s.ID);
    modelBuilder.Entity<HomeSection2>().Property(s => s.ID).ValueGeneratedOnAdd();
    modelBuilder.Entity<HomeSection2>().Property(s => s.Title).IsRequired();
    modelBuilder.Entity<HomeSection2>().Property(s => s.Header).IsRequired();

    //HomeSection2Detail
    modelBuilder.Entity<HomeSection2Detail>()
        .HasOne(p => p.HomeSection2)
        .WithMany(b => b.HomeSection2Detail);
    modelBuilder.Entity<HomeSection2Detail>().HasKey(s => s.ID);
    modelBuilder.Entity<HomeSection2Detail>().Property(s => s.ID).ValueGeneratedOnAdd();
    modelBuilder.Entity<HomeSection2Detail>().Property(s => s.Title).IsRequired();
    modelBuilder.Entity<HomeSection2Detail>().Property(s => s.Sequence).IsRequired();
}

And I have a generic repo

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
    protected readonly DbContext Context;

    public Repository(DbContext context)
    {
        Context = context;
    }

    public IEnumerable<TEntity> GetAll()
    {
        return Context.Set<TEntity>().ToList();
    }
}

When I call GetAll from the Application var obj = _uow.HomeSection2s.GetAll() like this, it won't fill the Detail.

tickwave
  • 3,335
  • 6
  • 41
  • 82
  • So use `Include`. – Gert Arnold Jun 17 '18 at 13:55
  • Consider returning IQueryable rather than IEnumerable with the .ToList(). By using IQueryable your consumer can decide what to do with the entities, including performing a .Include(), selecting a subset of data, doing a count, exists check with .Any() etc. Alternatively you need to pass a parameter to your methods to indicate which expanded properties you want to include, then add .Include() inside the repo method for each. – Steve Py Jun 17 '18 at 22:53

1 Answers1

-1

What you mean is reffered to as 'Lazy Loading'. It would require you to make those properties virtual, like:

public virtual List<HomeSection2Detail> HomeSection2Detail { get; set; }

You can also take a look at this anwser

More documentation on loading related data

Stephan
  • 2,356
  • 16
  • 38
  • based on your answer, on this part `return Query(eager).SingleOrDefault(i => i.EntityId == itemId);`. What is `EntityId` referring to? – tickwave Jun 17 '18 at 09:37
  • It's selecting the entity with that id ... In your case that example would read `i => i.ID == someValue` – pinkfloydx33 Jun 17 '18 at 09:46
  • the First method in the sample is for getting multiple items, the second is if you only want one. (By id). It assumes that all models have an EntityId (which it the primary key) – Stephan Jun 17 '18 at 09:47