We had a similar problem where an entity class is added to a collection in another class without the navigation properties being set and this was being stored in a shared DbContext.
We decided to detach the entity from context and then requery the Db if the we needed to reload the navigation properties (like these solutions: Force reload of entity in Entity Framework after insert/add and http://dev-doc.blogspot.co.uk/2015/09/entity-framework-reload-newly-created.html)
Here's an example of the code we used:
public class EntityRetriever
{
private readonly System.Data.Entity.DbContext _context;
public EntityRetriever(System.Data.Entity.DbContext context)
{
_context = context;
}
public T RetrieveEntity<T>(params object[] entityKeyValues) where T : class
{
var entity = _context.Set<T>().Find(entityKeyValues);
//check if is not an EF proxy class or has changed
if (entity != null
&& !IsProxy(entity)
&& _context.Entry(entity).State == System.Data.Entity.EntityState.Unchanged)
{
//This is the important part to reload navigation properties:
//detach entity from context and reload from db
_context.Entry(entity).State = System.Data.Entity.EntityState.Detached;
entity = _context.Set<T>().Find(entityKeyValues);
}
return entity;
}
public static bool IsProxy(object entityObject)
{
return entityObject != null &&
System.Data.Entity.Core.Objects
.ObjectContext.GetObjectType(entityObject.GetType()) != entityObject.GetType();
}
}