2016-07-18 4 views
0

私はAutomapperで実際に私の髪を引き出しています。私はそれが直感的ではなく、使いにくいことがわかります。マッピングコードを手作業で書くのはずっと簡単です。ビジネスオブジェクトをデータオブジェクトにマップできません

namespace BusinessObjects 
    { 
     public class Application 
     { 
      public int ID { get; set; } 
      public string FirstName { get; set; } 
      public string Surname { get; set; } 
      public System.DateTime DateOfBirth { get; set; } 
      public System.DateTime CreatedTime { get; set; } 
      public System.DateTime ModifiedTime { get; set; } 
      public System.DateTime ApplicationDate { get; set; } 
      public string Qualified { get; set; } 
      public int salary { get; set; } 
      public string userID { get; set; } 
    } 
    } 

namespace DataObjects 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class Application 
    { 
     public int ID { get; set; } 
     public string FirstName { get; set; } 
     public string Surname { get; set; } 
     public System.DateTime DateOfBirth { get; set; } 
     public System.DateTime CreatedTime { get; set; } 
     public System.DateTime ModifiedTime { get; set; } 
     public System.DateTime ApplicationDate { get; set; } 
     public string Qualified { get; set; } 
     public int salary { get; set; } 
     public string userID { get; set; } 
    } 
} 

とコントローラ:

public ApplicationController() 
     { 
      Mapper.Initialize(cfg => cfg.CreateMap<BusinessObjects.Application, ApplicationModel>()); 
      config = new MapperConfiguration(cfg => 

      { 
       cfg.CreateMap<BusinessObjects.Application, ApplicationModel>(); 
      }); 
      config.AssertConfigurationIsValid(); 

      Mapper.Initialize(cfg => cfg.CreateMap<ApplicationModel,BusinessObjects.Application>()); 
      config2 = new MapperConfiguration(cfg => 

      { 
       cfg.CreateMap<ApplicationModel,BusinessObjects.Application>(); 
      }); 
      config2.AssertConfigurationIsValid(); 

     } 

    public int Create(BusinessObjects.Application businessApplication) 
      { 
       var mapper =config2.CreateMapper(); 
       DataObjects.Application dataApplication = AutoMapper.Mapper.Map<DataObjects.Application>(businessApplication); 

       int count = 0; 
       using (CreditCardPreQualificationEntities CreditCardPreQualificationDatabase = new CreditCardPreQualificationEntities()) 
       { 
        CreditCardPreQualificationDatabase.Applications.Add(dataApplication); 
        count = CreditCardPreQualificationDatabase.SaveChanges(); 
       } 
       return count; 
      } 

私はこの作業を取得しようとしているほぼすべての週末を過ごしませんし、何より近い午前私は2つの単純な種類があります。私はAutomapperの利点を全く見ることができません。 "AutoMapper.AutoMapperMappingException型の例外はAutoMapper.dllで発生しましたが、ユーザーコードでは処理されませんでした。

追加情報:タイプマップの設定またはサポートされていないマッピングが見つかりませんでした。

+0

...初期化コードも非常に混乱します。アプリの開始時にAutoMapperを一度設定するだけで、各タイプマップを一度作成するだけで済みます。 – stuartd

+0

@stuartd、初期化コードはどこに入れますか?私はそれをビジネスオブジェクトからデータオブジェクトに1回、データオブジェクトからビジネスオブジェクトに1回ずつ2回入力しました。それは間違っていますか? – w0051977

+0

MVCアプリケーションのApplication_Start()にあります。 AutoMapperは一度のみ初期化する必要があります。**は古い構文と新しい構文のいずれかを使用します。古い方法は、 'Mapper.Initialize(cfg => { cfg.CreateMap (); cfg.CreateMap (); }});と' Mapper.Map (businessApplication); ' – stuartd

答えて

1

あなたの設定はオフになっています。ドキュメントで言うように、AppDomain全体でAutoMapperを1回だけ初期化する必要があります。あなたのglobal.asax(または同様のもの)に次のように追加してください:

Mapper.Initialize(cfg => { 
    cfg.CreateMap<BusinessObjects.Application, DataObjects.Application>() 
     .ReverseMap(); 
}); 

逆方向マップを追加したので、両方向で地図が必要なようです。その後、あなたのコントローラで、その初期化とマッパー作成の迷惑メールをすべて削除して、そのままAutoMapperを使用してください:

public int Create(BusinessObjects.Application businessApplication) 
     { 
      DataObjects.Application dataApplication = Mapper.Map<BusinessObjects.Application, DataObjects.Application>(businessApplication); 

      int count = 0; 
      using (CreditCardPreQualificationEntities CreditCardPreQualificationDatabase = new CreditCardPreQualificationEntities()) 
      { 
       CreditCardPreQualificationDatabase.Applications.Add(dataApplication); 
       count = CreditCardPreQualificationDatabase.SaveChanges(); 
      } 
      return count; 
     } 

完了!

+0

ありがとうございました。私はコンストラクタで設定をしていて、設定は上書きされていました。私は昨年早く解決しました。 – w0051977

+0

最近のAutomapperの質問をご覧ください:https://stackoverflow.com/questions/44973239/automapper-without-the-new-keyword? – w0051977

関連する問題