I am using virtual
keyword for some of my properties for EF lazy loading. I have a case in which all properties in my models that are marked as virtual
in case you wanna do it without needing to configure a second instance of automapper :
public static class ClearNavigationExtension
{
public static void ClearNavigation(this object entity)
{
var type = entity.GetType();
foreach (var property in type.GetProperties().Where(p => p.GetGetMethod().IsVirtual))
{
property.SetValue(entity, null);
}
}
public static T ClearNavigation(this T entity)
{
var type = entity.GetType();
var entityCopy = Mapper.Map(entity);
foreach (var property in type.GetProperties().Where(p => p.GetGetMethod().IsVirtual))
{
property.SetValue(entityCopy, null);
}
return entityCopy;
}
}