I'm having issues retrieving navigation properties after an insert.
I'm saving data using this code, without setting the navigation properties that I do not want to change. For example :
var entity = new MyEntity
{
FirstId = 1,
FirstObject = null
SecondId = 1,
SecondObject = null
//...data to update
};
_context.Update(myEntity);
_context.SaveChanges();
Then if I try to access the navigation property it will be null (even if the main object is tracked after the savechanges). I tried to reload the data using:
_context.Entry(entity).State = EntityState.Detached;
entity = _context.Set<MyEntity>().Where(e => e.Id == entity.Id).First();
I've tried using reload too:
_context.Entry(entity).State = EntityState.Detached;
_context.Entry(entity).Reload();
Still, navigation properties are null.
I am using UseLazyLoadingProxies in context configuration. The only way to get the navigation property is to load it manually:
_context.Entry(entity).Reference(e=> e.FirstObject ).Load()
Is there a way to reload data from db (discarding all the cached data) after a SaveChanges()?