عنوان:

‫استفاده از Auto-ignore در AutoMapper


نویسنده: سالار ربال
تاریخ: ۱۳۹۴/۰۲/۱۱ ۱۴:۴۹
آدرس: www.dntips.ir

Auto-ignore non existing destination properties with AutoMapper 



مشاهده مطلب اصلی

نظرات

  • سالار ربال در ۱۳۹۴/۰۲/۱۳ ۱:۱۵
    نکته 
    اگر از Profile برای پیکربندی نگاشت‌ها استفاده میکنید ، مطلب گفته شده در سایت ثالث جواب نخواهد داد. به روش زیر عمل کنید
      public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
            {
                foreach (var property in expression.TypeMap.GetUnmappedPropertyNames())
                {
                    expression.ForMember(property, a => a.Ignore());
                }
                return expression;
            }
    و در کلاس Profile 
     public class UserProfile : Profile
        {
            protected override void Configure()
            {
                
                CreateMap<RegisterViewModel, ApplicationUser>()
                    .IgnoreAllNonExisting();
                   
            }
    
            public override string ProfileName
            {
                get { return this.GetType().Name; }
            }
        }