The best way to refresh entities in your context is to dispose your context and create a new one.
If you really need to refresh some entity and you are using Code First approach with DbContext class, you can use
public static void ReloadEntity<TEntity>(
this DbContext context,
TEntity entity)
where TEntity : class
{
context.Entry(entity).Reload();
}
To reload collection navigation properties, you can use
public static void ReloadNavigationProperty<TEntity, TElement>(
this DbContext context,
TEntity entity,
Expression<Func<TEntity, ICollection<TElement>>> navigationProperty)
where TEntity : class
where TElement : class
{
context.Entry(entity).Collection<TElement>(navigationProperty).Query();
}
Reference:
https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.reload(v=vs.113).aspx#M:System.Data.Entity.Infrastructure.DbEntityEntry.Reload
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…