0

これは私のソースクラス

public class Content :IAggregateRoot
    {
        public Guid Id { get; set; }
        public string HeaderImage { get; set; }
        public string AboutText { get; set; }
        public string Address { get; set; }
        public long Phone { get; set; }
        public long Mobile { get; set; }
        public DateTime CreationTime { get; set; }
    }

これが私のDestinationクラスです

public class AboutViewModel
    {
        public string AboutText { get; set; }
        public string Address { get; set; }
        public long Phone { get; set; }
        public long Mobile { get; set; }
    }

ソースの余分なプロパティを無視し、このメソッドを使用してAutomapperでソースを宛先にマップしたいだけです

public static AboutViewModel ConvertToAboutViewModel(this Content content)
        {
           // Mapper.CreateMap<Content,AboutViewModel>().ForMember(x=>x.AboutText,)
            return Mapper.Map<Content, AboutViewModel>(content);
        }

どうやってやるの??

4

1 に答える 1

1

これらのプロパティは宛先クラスに存在しないため、Automapper はデフォルトでこれらのプロパティを無視します。

実際にコードを実行しようとしましたか? 動作しない場合は、発生しているエラーまたは例外の詳細を投稿してください。

次のように基本的なマップを定義する必要もあります。

 Mapper.CreateMap<Content, AboutViewModel>();
 Mapper.CreateMap<AboutViewModel, Content>();

ConvertToAboutViewModel を呼び出す前に、上記のコードが実行されていることを確認してください

于 2013-05-23T18:45:46.997 に答える