2016-06-28 7 views
6

同じプロパティ名でプロパティタイプが異なる場合、どのようにマッピングを無視できますか? デフォルトではスローエラーです。automapper - 同じプロパティ名でプロパティタイプが異なる場合のマッピングを無視する - C#

Mapper.CreateMap<EntityAttribute, LeadManagementService.LeadEntityAttribute>(); 

Model = Mapper.Map<EntityAttribute, LeadManagementService.LeadEntityAttribute>(EntityAttribute); 

私は無視するプロパティ名を指定する方法を知っていますが、それは私が望むものではありません。

.ForMember(d=>d.Field, m=>m.Ignore()); 

今後、新しいプロパティが追加される可能性があります。だから私は、異なるデータ型を持つすべてのプロパティのマッピングを無視する必要があります。

+0

ます(OPT =>オプト.ForAllMembersを試してみました.Condition(IsValidType)));私の答えはソースコードの例を見てください。 – Vinod

答えて

4

あなたは無視されなければならない性質のために無視し使用する必要がありますタイプのすべてのプロパティを処理するため

Mapper.CreateMap<EntityAttribute, LeadManagementService.LeadEntityAttribute>() 
    ForMember(d=>d.FieldToIgnore, m=>m.Ignore()); 
+1

私は将来新しいプロパティを追加するかもしれません。だから私は異なるデータ型を持つすべてのプロパティのマッピングを無視する必要があります – JerryGoyal

+0

することはできません。あなたが行うことができるのは、問題のフィールドが追加された場合に警告を表示する自動テストを設定することです。 –

0

一つの方法は、.ForAllMembers(OPT => opt.Condition(IsValidTypeを)))を使用することです。 AutoMapperの使用法に新しい構文を使用しましたが、古い構文でも機能するはずです。

using System; 
using AutoMapper; 

namespace TestAutoMapper 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var mapperConfiguration = new MapperConfiguration(cfg => cfg.CreateMap<Car, CarDto>() 
       .ForAllMembers(opt => opt.Condition(IsValidType))); //and how to conditionally ignore properties 
      var car = new Car 
      { 
       VehicleType = new AutoType 
       { 
        Code = "001DXT", 
        Name = "001 DTX" 
       }, 
       EngineName = "RoadWarrior" 
      }; 

      IMapper mapper = mapperConfiguration.CreateMapper(); 
      var carDto = mapper.Map<Car, CarDto>(car); 
      Console.WriteLine(carDto.EngineName); 

      Console.ReadKey(); 
     } 

     private static bool IsValidType(ResolutionContext arg) 
     { 
      var isSameType = arg.SourceType== arg.DestinationType; //is source and destination type is same? 
      return isSameType; 
     } 
    } 

    public class Car 
    { 
     public AutoType VehicleType { get; set; } //same property name with different type 
     public string EngineName { get; set; } 
    } 

    public class CarDto 
    { 
     public string VehicleType { get; set; } //same property name with different type 
     public string EngineName { get; set; } 
    } 

    public class AutoType 
    { 
     public string Name { get; set; } 
     public string Code { get; set; } 
    } 
} 
+0

"ForAllMembers"はvoid型のオペランドには適用できません – JerryGoyal

+0

詳細を投稿したり質問をあなたは試しましたか? – Vinod

3

あなたはセットアップに適切なマッピング条件をForAllMembers()を使用することができます。

Mapper.Initialize(cfg => 
{ 
    cfg.CreateMap<EntityAttribute, LeadEntityAttribute>().ForAllMembers(memberConf => 
    { 
     memberConf.Condition((ResolutionContext cond) => cond.DestinationType == cond.SourceType); 
    }); 
} 

あなたはまたForAllMaps()を使用してグローバルに適用することができます。

Mapper.Initialize(cfg => 
{ 
    // register your maps here 
    cfg.CreateMap<A, B>(); 

    cfg.ForAllMaps((typeMap, mappingExpr) => 
    { 
     var ignoredPropMaps = typeMap.GetPropertyMaps(); 

     foreach (var map in ignoredPropMaps) 
     { 
      var sourcePropInfo = map.SourceMember as PropertyInfo; 
      if (sourcePropInfo == null) continue; 

      if (sourcePropInfo.PropertyType != map.DestinationPropertyType) 
       map.Ignore(); 
     } 
    }); 
}); 
+0

私は2番目のオプション 'ForAllMaps()'を試して、 'map.Ignore()'の最後の行にエラーが発生しています。 'Ignore'メソッドが見つかりません。このエラーを取得するには 'CS1061 \t' PropertyMap 'に' Ignore 'の定義がなく、' PropertyMap 'タイプの最初の引数を受け入れる拡張メソッド' Ignore 'が見つかりませんでした(usingディレクティブまたはアセンブリ参照?) '思考? – Shiva

+0

@ Shiva、代わりに 'map.Ignored = true'を試してください。おそらく彼らは答えが書かれてからAPIを変更しました。 – haim770

関連する問題