Asked by:
Child tables lookup is not loading using Lazy loading

Question
-
User-543160537 posted
Hi,
I have a primary table and secondary table lookup where i will load secondary drop down values based on primary drop down selection. But secondary drop down is not loading when i am using lazy loading technique.
List<pr> rs = LookupSingleton.Instance.PReasonLookup .Where(x => x.StartDate != null)) .ToList(); foreach (Preasonclass pr in rs) { List<SecondaryReason> secondaryReasons = pr.SecondaryReasons -- getting error in this line .Where(x => x.StartDate != null)) .OrderBy(x => x.Description).ToList(); }
Can some on please advice ?
Thanks
Monday, March 19, 2018 2:12 PM
All replies
-
User475983607 posted
There are logical errors in the posted code.
The example is not lazy loading. The ToList() invokes the query and fills rs. At that point the collection is in memory.
The second block of code, if working as intended, would result in the the secondaryReasons collection to be filled by the last item in the rs collection.
Lastly, the code hints to some kind of singleton instance which can be problematic too.
Can you explain the code and what you are trying to do?
Monday, March 19, 2018 2:50 PM -
User-543160537 posted
Hi,
Below is the code from my singleton class where we have lazy loading instance..
private static readonly Lazy<LookupSingleton> _lazy = new Lazy<LookupSingleton>(() => new LookupSingleton()); public static LookupSingleton Instance => _lazy.Value; private LookupSingleton() { } public IReadOnlyCollection<Pr> PrLookup { get { Func<List<PR>> getFunc = () => { using (DbEntities unitOfWork = new DbEntities()) { return unitOfWork.PrimaryReasons.ToList(); } }; return CacheManager.GetOrAddToCache("PrLookup", getFunc); } } public IReadOnlyCollection<SecondaryReason> SecondaryReasonLookup { get { Func<List<SecondaryReason>> getFunc = () => { using (DbEntities unitOfWork = new DbEntities()) { return unitOfWork.SecondaryReasons.ToList(); } }; return CacheManager.GetOrAddToCache("SRLookup", getFunc); } }
Thanks
Monday, March 19, 2018 3:02 PM -
User475983607 posted
First, what is the error?
Did you build this code yourself or copy it from another source? Can you explain what you are trying to do?
Monday, March 19, 2018 3:17 PM -
User-543160537 posted
Below is the error i am getting..
SecondaryReasons = '((System.Data.Entity.DynamicProxies.PrimaryReason_E4398F38928B5F427CE6634ECE8D5E2B2B0FACB1CF59DCDA84B430AA83430561)new System.Collections.Generic.Mscorlib_CollectionDebugView<my.Data.Pr>(rs).Items[0]).SecondaryReason...
I have below Virtual method generated by EF..
public virtual ICollection<SecondaryReason> SecondaryReasons { get; set; }
List<pr> rs = LookupSingleton.Instance.PReasonLookup .Where(x => x.StartDate != null)) .ToList(); foreach (Preasonclass pr in rs) { List<SecondaryReason> secondaryReasons = pr.SecondaryReasons -- getting error in this line .Where(x => x.StartDate != null)) .OrderBy(x => x.Description).ToList(); }
I am trying to load the secondary reasons which is in rs list.
Monday, March 19, 2018 7:41 PM -
User1120430333 posted
SecondaryReasons = '((System.Data.Entity.DynamicProxies.PrimaryReason_E4398F38928B5F427CE6634ECE8D5E2B2B0FACB1CF59DCDA84B430AA83430561)new System.Collections.Generic.Mscorlib_CollectionDebugView<my.Data.Pr>(rs).Items[0]).SecondaryReason...
That's the error message? It seems that you have not posted the exception message.
Monday, March 19, 2018 8:54 PM -
User-166373564 posted
Hi sreekanth,
As DA924 said, please post the details error message. Besides, I suggest you could check whether you enable the Lazy loading.
Here are some relevant articles about how to use Lazy loading, you could refer to them:
https://docs.microsoft.com/en-us/ef/core/querying/related-data#lazy-loading
http://www.entityframeworktutorial.net/lazyloading-in-entity-framework.aspx
Besides, if still not working, I suggest you could try to use Eagerly Loading to load related data.
https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx
Best regards,
AngieSunday, March 25, 2018 6:40 AM