If I remove virtual keyword from navigation property on an entity and set LazyLoadingEnabled to false in DbContext , the loaded entity still shows navigation properties for GetAllListAsync methods from the repository. Shouldn't it be showing null for the navigation properties?
4 Answer(s)
-
0
Hi @sparkyjr,
I assume it needs to act like you said. Can you share your related code ? By the way, you are using EF 6.x, right ?
-
0
Yes I am using EF 6.0.
The following is my code snippet: I am setting LazyLoadingEnabled to false in DbContext.
public ABXDbContext() : base("Default") { Configuration.LazyLoadingEnabled = false; } public ABXDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { Configuration.LazyLoadingEnabled = false; }
Here are my entities with virtual removed:
public class CascadeLevel : FullAuditedEntity, IMustHaveTenant { [Required] public int TenantId { get; set; } [Required] [MaxLength(100)] public string Name { get; set; } [Required] public int Level { get; set; } public ICollection<CascadeLevelType> CascadeLevelTypes { get; set; } } public class CascadeLevelType : FullAuditedEntity, IMustHaveTenant { [Required] public int TenantId { get; set; } [Required] public int CascadeLevelId { get; set; } public CascadeLevel CascadeLevelData { get; set; } [Required] [MaxLength(100)] public string Name { get; set; } [MaxLength(500)] public string Description { get; set; } public string IconClass { get; set; } public string AssociationId { get; set; } //Gaurav(1) public ICollection<CascadeLevelTypeLabelValueRelation> CascadeLevelTypeLabelValueRelations { get; set; } public ICollection<CascadeLevelTypeKPIRelation> CascadeLevelTypeKPIRelations { get; set; } public ICollection<Initiative> Initiatives { get; set; } } private readonly IRepository<CascadeLevel> _cascadeLevelRepository; private readonly IRepository<CascadeLevelType> _cascadeLevelTypeRepository; public async Task<GetCascadeLevelsDto> GetAllCascadeLevels() { GetCascadeLevelsDto cascadeLevelsDto = new GetCascadeLevelsDto(); var allCascadeLevels = await _cascadeLevelRepository.GetAllListAsync(); var allCascadeLevelTypes = await _cascadeLevelTypeRepository.GetAllListAsync();
-
0
Is your breakpoint on the foreach statement?
Since you have executed _cascadeLevelTypeRepository.GetAllListAsync(), EF fills the collection property for you.
Try putting the breakpoint on that statement instead. The collection property should be null.
-
0
hi,
if you declare it non-virtual : Your property will (by default) be loaded right away along with all the other property in your main entity. This means your property will be ready to access : it has already been retreived. Entity won't have to query again the database because you access this property.
<a class="postlink" href="https://stackoverflow.com/a/41881389/1767482">https://stackoverflow.com/a/41881389/1767482</a>