When I was trying to fetch the child of the datasource item, it is getting a null value for the child. After so many r&d I am getting to know about Lazy loading and this issue is coming because of enabling lazy loading. So, I am trying to turn off lazy loading by the below method.
[SitecoreChildren(IsLazy = false)]
IEnumerable
The above code is working only if you are using below v5 of glass mapper. But in version 5 or more (v5), the glass mapper has removed this method from here. So we can use an alternative method to turn off lazy loading. Please check the below code.
var datasource = _mvcContext.GetDataSourceItem(new Glass.Mapper.Sc.GetKnownOptions() { Lazy = Glass.Mapper.LazyLoading.Disabled });
I resolved my issue by doing this method.
These changes are working for me but after some days I am facing the below kind of error and it tells me to enable lazy loading, and by enabling lazy loading I do not get children of the datasource.
Below is my error please check.
Model depth check failed. Model graph too large, enable lazy loading. Type requested:
Sitecore.Data.Items.Itemlocal.Sc.Models.Templates.UserDefined.Main.Entities.Lists.IPublicationType
local.Sc.Models.Templates.UserDefined.Main.Entities.ExperienceAndAnalysis.IExperienceAndAnalysisBase
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.UserDefined.Main.Pages.ICapabilitiesLanding
Description: An unhandled exception occurred.
Exception Details: Glass.Mapper.MapperStackException: Model depth check failed. Model graph too large, enable lazy loading. Type requested:
Sitecore.Data.Items.Itemlocal.Sc.Models.Templates.UserDefined.Main.Entities.Lists.IPublicationType
local.Sc.Models.Templates.UserDefined.Main.Entities.ExperienceAndAnalysis.IExperienceAndAnalysisBase
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IServicew
local.Sc.Models.Templates.UserDefined.Main.Pages.ICapabilitiesLanding
By doing R&D, I got an option which is given in lazy loading “OnlyReferenced”. I applied this option on lazy loading and then my issue was resolved.
var datasource = _mvcContext.GetDataSourceItem<IInterfaceClass>(new Glass.Mapper.Sc.GetKnownOptions() { Lazy = Glass.Mapper.LazyLoading.OnlyReferenced });
This error occurred because lazy loading disables the item nested child and so on till the 8th child comes after the 8th child will not be permitted.
Lazy loading disabled: Lazy loading is disabled for the current model and all referenced Glass models.
Lazy loading Only reference: Lazy loading is disabled for the current model but all referenced model will be lazy loaded.