My context looks like:
public class ApplicationDbContext: IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { this.Configuration.LazyLoadingEnabled = true; } //DbSet properties } so, lazy loading is enabled.
I have following class:
public class Home { private ICollection<Slide> _slides; [Key] [Required] public string Name { get; set; } [ForeignKey("Header")] public int? HeaderID { get; set; } //Navigation properties public ICollection<Slide> Slides { get { return _slides ?? (_slides = new List<Slide>()); } set { _slides = value; } } public Content Header { get; set; } } Note that both navigation properties Header and Slides are used without virtual keyword. As far as I know when we don't use virtual keyword - properties should load eagerly.
However, when I get my Home entity from database, both my navigation properties are null (but property HeaderID has value).
Even if I switch to this.Configuration.LazyLoadingEnabled = false; - preperties not loaded - they still null.
Here is how I get my data from db (using repository pattern):
public static Home GetHomeComponent( this IRepositoryAsync<Home> repository) { var result = repository .Query() .Select() .First(); return result; } I solved my problem with Include properties:
public static Home GetHomeComponent( this IRepositoryAsync<Home> repository) { var result = repository .Query() .Include(x => x.Header) .Include(x=>x.Slides) .Select() .First(); return result; } However it's not convenient for me (since I have too much navigation properties to load).
So, my question is:
I don't use virtual keyword - but why my navigation properties not loading eagerly?
Or I'm doing something wrong? Is there any other way to load my navigation properties without using Include?
virtualkeyword can control how my data loads - lazy or eagerly, but for some reason it's not working (and my Q is why it's not working?)... Btw, as I mentioned in my question, even swithcing tothis.Configuration.LazyLoadingEnabled = false;doesn't solve my problem - properties still returnsnull.