How to check if property is already lazy loaded?
That error will occur whenever you explicitly or implicitly attempt to lazy-load an entity outside of the scope of the DbContext that it is associated to. "Checking if it's lazy loaded or not" really just amounts to turning off lazy loading, in the sense that you'll either get the entity (if it had been loaded prior to leaving the scope of the DbContext) or you will get #null.
Essentially if you do this: "This exception can be suppressed or logged by passing event ID 'CoreEventId.DetachedLazyLoadingWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'A ddDbContext'." on the DbContext configuration, it should return #null if the lazy load cannot be done rather than throw that exception.
If you are going to use lazy loading it is important to ensure that an entity is always accessed while within the scope of a DbContext
. Either the DbContext
where it was read, or having been detached from that context and attached to a new DbContext
instance that it can call upon to perform any possible lazy loading.