i rare use EF. so i have a question. if we do not use virtual keyword with dbset then lazy load will not work?
i read a article from this link https://stackoverflow.com/a/24114284/5314244 they provide code like
public class AppContext : DbContext
{
public AppContext()
{
Configuration.LazyLoadingEnabled = true;
}
public virtual DbSet<AccountType> AccountTypes { get; set; }
}
public class AccountType
{
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<AccountCode> AccountCodes { get; set; }
}
public class AccountCode
{
public Guid Id { get; set; }
public string Name { get; set; }
public Guid AccountTypeId { get; set; }
public virtual AccountType AccountType { get; set; }
}
they said :
The virtual keyword on the navigation properties are used to enable lazy loading mechanism, but the LazyLoadingEnabled property of the configuration must be enabled.
The virtual keyword on AccountType::AccountCodes navigation property will load all account codes the moment there is a programmatically access to that property while the db context are still alive
if we declare this code without virtual keyword public DbSet<AccountType> AccountTypes { get; set; }
then when this line execute var accountCodes = accountType.AccountCodes;
what will happen?
Error will thrown or null will be stored in accountCodes
variable?
second question what is default in EF---> lazy load or eager loading? how many type of loading option available ?
thanks