2015-01-18 8 views
1

최신 버전의 AutoMapper를 사용하고 있습니다. global.asaxDto를 ViewModel에 매핑 할 수 없습니다.

에라고 내 automapper 부트 스트 래퍼 클래스의

내가 가진 Mapper.CreateMap<ArticleDto, EditViewModel>(); 다음은 동일한의 전체 목록입니다 :

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 

는 예외 detials 무엇 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 
+0

이 답변은 단서가있을 수 있습니다 : 우리의 응용 프로그램에서 특정 모듈, Automapper 다시 초기화되고 있었다 입력과의 Global.asax에서 만든 내 매핑이 있었다 열려있는 때 또는 http://stackoverflow.com/a/14280633/1559978을 " 제거되었습니다. " - global.asax에서 매핑이 제거 될 수 있습니까? –

+0

오류가 포함되도록 제 질문이 업데이트되었습니다. 나는 SimpleInjector IOC를 사용하고있다. 내 코드가 AutoMapperTypeAdapter를 사용하여 아무 것도하지 않습니다. – NoobDeveloper

답변

3

두 가지 문제 :

  1. 두 번 초기화를 호출하지 마십시오. 초기화는 구성을 재설정합니다.

첫 번째는 문제를 일으키는 것입니다 Mapper.CreateMap하지, 프로필에 기본 CreateMap 메서드를 호출하여, 두 번째는 내가 당신의 설정에서 본 뭔가입니다.

+0

굉장합니다. 사랑해. 놀라운 도서관. EF 확장으로 단순성과 연결성이 좋아졌습니다. – NoobDeveloper

+0

고마워, 고마워! –