2017-12-14 7 views
0

はい、これはもう1つの「オートマッパーがマッピングしていません」という質問です。何かが壊れた、または私はそれについて愚かな方法を行っています。 3.1.0でテストしましたが、AutoMapper 3.2.0(最新の安定リリース)を使用してASP.NET Core 2.1でwebappを構築しています。Automapperは2つのオブジェクト間のマッピングではありません(すべての目的と目的で実質的に同じです)。

質問別にマッピングする

シンプルオブジェクト。テストと試行のために、これらはまったく同じですが、オートマトンは次のものを返します。

AutoMapperMappingException:タイプマップの設定が欠落しているか、サポートされていないマッピングです。

マッピングタイプ: NotificationModel - > NotificationViewModel ProjectName.Models.Dashboard.NotificationModel - > ProjectName.Models.Dashboard.NotificationViewModel

奇妙なことは、私が以前にこのモデルはで日曜日に7つの方法を設定してマッピングされている、あります唯一変化しているStartup.csファイルは私の表情です。他のマップは、同じコードではないにしても、同様のコードを使って示されたように動作します。

モデル

NotificationModel.cs

public class NotificationModel 
    { 
     public int Id { get; set; } 
     public string Content { get; set; } 
     public DateTime CreateTS { get; set; } 
     public bool FlagRead { get; set; } 
     public bool FlagSticky { get; set; } 
     public bool FlagReceipt { get; set; } 
     public string ReceiptContact { get; set; } 
     public string UserId { get; set; } 
     public bool CANCELLED { get; set; } 
    } 

NotificationViewModel.cs

public class NotificationViewModel 
{ 
    public int Id { get; set; } 
    //Reminder, this model has been amended to exactly represent that of the original model for testing purposes. 
    public string Content { get; set; } 
    public DateTime CreateTS { get; set; } 
    public bool FlagRead { get; set; } 
    public bool FlagSticky { get; set; } 
    public bool FlagReceipt { get; set; } 
    public string ReceiptContact { get; set; } 
    public string UserId { get; set; } 
    public bool CANCELLED { get; set; }   
} 

&起動Automapper構成

Mapper.Initialize(cfg => 
      { 
// Some other mappings removed for clarity. 
       cfg.CreateMap<GroupViewModel, GroupModel>().ReverseMap(); 
       //cfg.CreateMap<EntityViewModel, EntityModel>().ReverseMap().ForAllOtherMembers(opt => opt.Ignore()); 


       cfg.CreateMap<NotificationModel, NotificationViewModel>().ForAllMembers(opt => opt.Ignore()); 
       cfg.CreateMap(typeof(NotificationViewModel), typeof(NotificationModel)); 
//I even left out the .ReverseMap, for testing purposes. 
      }); 
      Mapper.AssertConfigurationIsValid(); 

使用

NotificationViewModel test = _mapper.Map<NotificationViewModel>(item); << Which私は例外を受け取る場所です。

他の試み

[OK]をので、私は別のものを説明するいくつかのより多くの記事を完了して、その後、それぞれ以下を試してみた:とともに

cfg.CreateMap(typeof(NotificationModel), typeof(NotificationViewModel)); 
cfg.CreateMap<NotificationModel, NotificationViewModel>().ReverseMap().ForAllMembers(opt => opt.Ignore()); 
cfg.CreateMap<NotificationModel, NotificationViewModel>().ForAllOtherMembers(opt => opt.Ignore()); 

NotificationViewModel test = _mapper.Map<NotificationViewModel>(item); 
_mapper.Map(item, typeof(NotificationViewModel), typeof(NotificationModel)); 
NotificationViewModel existingDestinationObject = new NotificationViewModel(); 
       _mapper.Map<NotificationModel, NotificationViewModel>(item, existingDestinationObject); 

私は.Map()/。Map <>使い方を修正しようとしましたが、いずれもyielに見えませんでした何も設定されていないという例外があります。

このオブジェクトの変換を手作業で書くのは簡単ではありません(その目的のためには簡単です)。ここでは解決策が必要です。使用しない場合は、教えて他の人が同じように直面するのを助けてください。 ITワークス

UPDATE

!プロジェクトを通じて

スキャン、私はどこか、以前のドキュメントのことに気づいた - 私はちょうどプロフィールと呼ばれる抽象クラスを継承し、「設定」クラスの型を作成する方法についてお読みください。このクラスではまた、あなたのマップを定義することができ、まだ何奇妙なことは、私はこのクラスをドロップすると、単に私のStartup.csファイルで設定マップの設定を使用することはできませんよということであるだろう。 Automapperは、この別のクラスで定義されていないマップを保持することを拒否します。以下は、しかし、私はまだそれなしで、必要に応じてAutomapperが機能しない理由として説明を必要とし、私は必要なものを私を得るようだ:

public class AMConfig : Profile 
{ 
    public AMConfig() 
    { 
     CreateMap<ManageUserModel, IndexViewModel>(); 
     CreateMap<IndexViewModel, ManageUserModel>(); 
     CreateMap<NotificationViewModel, NotificationModel>().ReverseMap(); 
     CreateMap<List<NotificationViewModel>, List<NotificationModel>>().ReverseMap(); 
     CreateMap<TaskViewModel, TaskModel>().ReverseMap(); 
    } 
} 

ありがとう!プロジェクトを通じて

+0

あなたは 'Mapper.Map (アイテムを使用する場合、それは動作します) '? –

+0

いや、_mapperは私がAutomapperと対話するために使用する必要がDIオブジェクトです。もう一度プラス。これは他のコントローラで動作します..しかし、提案に感謝します。私は尋ねた – Medismal

+0

理由は、あなたが*静的*マッパーを設定して、*非静的*マッパーを使用しているようだということですので、私は設定はあなたが提供した情報に基づいて双方の間で動作するように期待していません質問。 –

答えて

0

スキャン、私はどこか、以前のドキュメントのことに気づいた - 私はちょうどプロフィールと呼ばれる抽象クラスを継承し、「設定」クラスの型を作成する方法についてお読みください。このクラスではまた、あなたのマップを定義することができ、まだ何奇妙なことは、私はこのクラスをドロップすると、単に私のStartup.csファイルで設定マップの設定を使用することはできませんよということであるだろう。 Automapperは、この別のクラスで定義されていないマップを保持することを拒否します。下記しかし、私はまだそれなしで、必要に応じてAutomapperが機能しない理由として説明を必要とし、私は必要なものを私を得るようだ:

public class AMConfig : Profile 
{ 
    public AMConfig() 
    { 
     CreateMap<ManageUserModel, IndexViewModel>(); 
     CreateMap<IndexViewModel, ManageUserModel>(); 
     CreateMap<NotificationViewModel, NotificationModel>().ReverseMap(); 
     CreateMap<List<NotificationViewModel>, List<NotificationModel>>().ReverseMap(); 
     CreateMap<TaskViewModel, TaskModel>().ReverseMap(); 
    } 
} 
関連する問題