0

I wanted to eager load a collection but it does not returns any data

I have tried nothing

//here is my controller

var accountGroup = await db.AccountGroups.Include(a=>a.AccountGroupTypes)
.Include(a=>a.AccountPrimaryGroups).ToListAsync();

//here is my model

public int? Id { get; set; }
public string Name { get; set; }
public int AccountGroupTypeId { get; set; }
public ICollection<AccountGroupType> AccountGroupTypes { get; set; }

public int AccountPrimaryGroupId { get; set; }
public ICollection<AccountPrimaryGroup> AccountPrimaryGroups { get; set; }

public DateTime CreationDateUtcNow{ get; set; }

It returns AccountGroupData but It does not return AccountGroupTypes and AccountPrimaryGroup data

taygetos
  • 3,005
  • 2
  • 21
  • 29
  • [Log the SQL](https://learn.microsoft.com/en-us/ef/ef6/fundamentals/logging-and-interception) and run it against your database. – Steve Greene Jun 18 '19 at 18:36

1 Answers1

0

Mark your AccountGroupTypes and AccountPrimaryGroups as virtual properties and be sure that you have a parameterless constructor which is initializing those properties.

public int? Id { get; set; }
public string Name { get; set; }
public int AccountGroupTypeId { get; set; }
public vitual ICollection<AccountGroupType> AccountGroupTypes { get; set; }
public int AccountPrimaryGroupId { get; set; }
public virtual ICollection<AccountPrimaryGroup> AccountPrimaryGroups { get; set; }
public DateTime CreationDateUtcNow{ get; set; }

public AccountGroup()
{
    this.AccountGroupTypes = new HashSet<AccountGroupType>();
    this.AccountPrimaryGroups = new HashSet<AccountPrimaryGroups>();
}
  • Virtual is for lazy loading. You also don't need to `new` up the collections. See [here](https://stackoverflow.com/questions/20757594/ef-codefirst-should-i-initialize-navigation-properties). – Steve Greene Jun 18 '19 at 19:14