I have a problem that I believe will find many professional developers. An entity structure was adopted at my workplace. We use it and love. However, it seems that we are faced with a very frustrating restriction.
Suppose you have a whole chain of objects
A → B → C → D
We are professionals, therefore these objects have a lot of data, and there is a lot of them in the corresponding database tables. It seems that EF is scary loading something behind object B. The SQL queries that it generates are really inefficient and not very good. The call will look like
context.objects.include("bObjectName.cObjectName.dObjectName").FirstOrDefault(x => x.PK == somePK);
We went around this by explicitly loading objects for this second level using the .Load () command. This works well for individual objects. However, when we talk about a set of objects, we begin to encounter problems with .Load ().
Firstly, there seems to be no way to keep proxy tracking of objects in a collection without the virtual keyword. This makes sense because it needs to overwrite the get and set functions. However, this provides lazy loading, and .Load () does not display objects when lazy loading is enabled. I think this is somewhat strange. If you delete the virtual keyword, .Load () automatically associates the loaded objects with the corresponding objects in the context.
So, here is the gist of my problem. I want a proxy to track, but also want .Load () to display the navigation properties for me. None of this would be a problem if EF could generate good requests. I understand why this cannot be, it should be one show that fits all things.
So, in order to load the third level of objects, I could create a loader function at my service level, which takes all the primary keys of the second level of objects and then calls .Load () on them. Does anyone have a solution for this? It seems that EF7 or Core 1.0 solves this:
- Completely removing the lazy boot that we could disable, but that would break the old code.
- Adding a new function "ThenInclude", which supposedly improves the efficiency of the chain, includes massive.
If disabling lazy loading is the answer, that's fine, I just want to exhaust all the options before spending a lot of time rebuilding huge cost service requests. Does anyone have any ideas? I am ready to do something. We use EF6.
EDIT: It seems the answer is to disable lazy loading at the context level or switch to EF7. I will change this if anyone else succeeds in finding a solution thanks to which you can track proxies with forced loading of one object for EF6.