'EF manually load / populate navigation property

I have the below code that fetches the data from service and attaches to the objects that was loaded from local database by EntityFramework 6!

We had the UserJob details before in our local database, However we decided to move it to the service later and migrated all the UserJobs to new micro service.

We are in the middle of this move and now we are saving the User jobs in both local database and in the new service!

When we read from micro service and update the local database as well, the code treats its a new job, since the JobId is no longer available in new service, but we have same creation date and user id to find the JobId available in local database! I set it now correctly.

private void IncludeUserJobOnUser(UserJob userJob, MyDBContext db)
{
var aspnetUser = new EFDataReader().GetUser(userJob.userId, db);
if (userJob != null && aspnetUser != null)
{        
    userJob.JobId = new EFDataReader().GetUserJobId(userJob.userId, userJob.CreationDate, db);
    DbEntityEntry entry = db.Entry(userJob);
    entry.State = EntityState.Unchanged;
            
    if (aspnetUser.UserJobs.Any(x => x.JobId != userJob.JobId)) // Here is the problem
    {
        aspnetUser.UserJobs.Add(userJob);
    }
    userJob.AspNetUser = aspnetUser;
}

}

PROBLEM:

I am trying to set the navigational property with the objects retrieved from the microservice. However, EF6 always hits the database and populate them from local database instead adding the entity came from microservice.

Any idea how to solve the above?



Solution 1:[1]

If you mean that by accessing "aspnetUser.UserJobs" you are seeing a query kicked off to load the UserJobs as part of the Any() check, that would be Lazy Loading. If the UserJobs were already eager loaded when the data reader loaded the user then this wouldn't happen. If the user hasn't loaded the jobs, how would the code know if the user had that job or not?

Code like this can be dangerous at runtime if not careful because you are passing entity references around to be associated with other entities where the scope of the DbContext that may, or may not still be tracking these entities is not clearly defined. Provided that "db" DbContext instance is the single overarching DbContext for retrieving all entities (including UserJob) are loaded from or associated to then you should be ok, but when I read things like "objects retrieved from the microservice" that sends up a bit of a red flag as Microservices would be scoping their own DbContext so returning entities from one would not be known by the current DbContext whose entities you are associating such results with.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Py