Entity Framework - lazy load through virtual keyword - detect circular references

.net c# entity-framework entity-framework-6

Question

Entity Framework lazy loads Collections in the background if they are marked with a virtual keyword (for proxying) and lazy loading is enabled on context. So for example:

public class Entity()
{
    public virtual ICollection<OtherEntity> OtherEntities { get; set; }
}

EF will load OtherEntities even if the relevant method fetching it from the database doesn't have an Include for this entity.

Knowing that it causes issues with serialization and it loads tons of unnecessary data into created results overriding whats in the serializer, I started removing the virtual keywords and adding includes in relevant repositories but we still tend to pop into the dreaded circular reference issue once every while with Created results. Some entities might have tens of collections inside so its difficult to debug and it doesn't even throw an exception but just cycles between entities consuming more and more memory. I am forced to manually track down where it starts cycling. This is time consuming and horribly unfun. Any idea if there is a better way to localize the Collections causing issues with serialization?

We are using newtonsoft for serialization as with 99% of .net projects.

0
0
9/21/2018 12:29:47 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow