1

AutoMapper の最新バージョンを使用しています。

Mapper.CreateMap<ArticleDto, EditViewModel>();で呼び出されるオートマッパーブートストラップクラスに持って いますglobal.asax

同じものの完全なリストは次のとおりです。

public static class AutoMapperConfig
    {
        public static void Configure()
        {
            Mapper.Initialize(cfg => cfg.AddProfile(new AutomapperWebConfigurationProfile()));
            Mapper.Initialize(cfg => cfg.AddProfile(new AutomapperServiceConfigurationProfile()));
        }
    }

    public class AutomapperWebConfigurationProfile : Profile
    {
        protected override void Configure()
        {

            Mapper.CreateMap<CreateArticleViewModel, ArticleDto>()
                .ForMember(dest => dest.Title, opts => opts.MapFrom(src => src.Title.Trim()))
                .ForMember(dest => dest.PostBody, opts => opts.MapFrom(src => src.PostBody.Trim()))
                .ForMember(dest => dest.Slug,
                    opts =>
                        opts.MapFrom(
                            src => string.IsNullOrWhiteSpace(src.Slug) ? src.Title.ToUrlSlug() : src.Slug.ToUrlSlug()))
                .ForMember(dest => dest.Id, opt => opt.Ignore())
                .ForMember(dest => dest.CreatedOn, opt => opt.Ignore())
                .ForMember(dest => dest.Author, opt => opt.Ignore());


            Mapper.CreateMap<ArticleDto, EditViewModel>()
                .ForMember(dest => dest.Categories, opt => opt.Ignore());



             Mapper.AssertConfigurationIsValid();
        }
    }

以下のマッピングの何が問題なのかを理解するのに 2 時間近く費やしました。

 public class ArticleDto
    {
        public int Id { get; set; }
        public string Slug { get; set; }
        public string Title { get; set; }
        public string PostBody { get; set; }
        public DateTime CreatedOn { get; set; }
        public bool IsPublished { get; set; }
        public string Author { get; set; }
        public List<string> ArticleCategories { get; set; }
    }



 public class EditViewModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string PostBody { get; set; }
        public DateTime CreatedOn { get; set; }
        public string Slug { get; set; }
        public bool IsPublished { get; set; }
        public IEnumerable<SelectListItem> Categories { get; set; }
        public List<string> ArticleCategories { get; set; }
    }

これが私のコントローラーのコードです。

// Retrive ArticleDto  from service
var articleDto = _engine.GetBySlug(slug);

// Convert ArticleDto to ViewModel and send it to view
var viewModel = Mapper.Map<EditViewModel>(articleDto);

この Dto から ViewModel への変換は失敗します。

誰かがこれについて私を助けることができますか? 例外の詳細はこちら

Exception Details: AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
ArticleDto -> EditViewModel
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel

Destination path:
EditViewModel

Source value:
NoobEngine.Dto.ArticleDto

これが Mapper.AssertConfigurationIsValid(); です。に結果

Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================================================
ArticleDto -> EditViewModel (Destination member list)
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel (Destination member list)

Unmapped properties:
Categories

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: AutoMapper.AutoMapperConfigurationException: 
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================================================
ArticleDto -> EditViewModel (Destination member list)
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel (Destination member list)

Unmapped properties:
Categories

上記のエラーで述べたように、マッピングを更新する方法は次のとおりです

  Mapper.CreateMap<ArticleDto, EditViewModel>()
                .ForMember(dest => dest.Categories, opt => opt.Ignore());

それでも、以下に示すのと同じエラーが発生します。

An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code

Missing type map configuration or unsupported mapping.

Mapping types:
ArticleDto -> EditViewModel
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel

Destination path:
EditViewModel

Source value:
NoobEngine.Dto.ArticleDto
4

1 に答える 1

3

コードの 2 つの問題:

  1. Initialize を 2 回呼び出さないでください。Initialize は構成をリセットします。
  2. Mapper.CreateMap ではなく、プロファイルで基本の CreateMap メソッドを呼び出します

1 つ目は問題の原因であり、2 つ目は構成で見たものです。

于 2015-01-19T14:08:04.380 に答える