如何忽略被标记为虚拟的所有属性 [英] How to ignore all properties that are marked as virtual
本文介绍了如何忽略被标记为虚拟的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用虚拟
关键字一些我的EF延迟加载特性。我有这样的情况,这在我的模型的所有属性标记为虚拟
应该映射源到目的地时,从AutoMapper忽略。
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
should be ignored from AutoMapper when mapping source to destination.
是否有自动的方式,我可以做到这一点,或者我应该手动忽略每一个成员?
Is there an automatic way I can achieve this or should I ignore each member manually?
推荐答案
您可以创建一个映射的扩展和使用它:
namespace MywebProject.Extensions.Mapping
{
public static class IgnoreVirtualExtensions
{
public static IMappingExpression<TSource, TDestination>
IgnoreAllVirtual<TSource, TDestination>(
this IMappingExpression<TSource, TDestination> expression)
{
var desType = typeof(TDestination);
foreach (var property in desType.GetProperties().Where(p =>
p.GetGetMethod().IsVirtual))
{
expression.ForMember(property.Name, opt => opt.Ignore());
}
return expression;
}
}
}
用法:
Mapper.CreateMap<Source,Destination>().IgnoreAllVirtual();
这篇关于如何忽略被标记为虚拟的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文