2014-09-17 17 views
8

Używam funkcjonalności Projektu w Automapper i Entity Framework, ale pojawia się problem, w którym Automapper nie chce wyświetlać jednego typu wyliczeniowego na inny.Automapper nie może wyświetlić jednego typu wyliczeniowego na inny

Mam następujące podmioty:

public class UserProfile 
{ 
    public Guid Id { get; set; } 
    public string Name { get; set; } 
    private HashSet<UserProfilePhone> m_Phones; 
    public virtual HashSet<UserProfilePhone> Phones 
    { 
     get { return m_Phones ?? (m_Phones = new HashSet<UserProfilePhone>()); } 
     set { this.m_Phones = value; } 
    } 
} 

public class UserProfilePhone 
{ 
    public PhoneType Type { get; set; } 
    public virtual string Number { get; set; } 
} 

public enum PhoneType 
{ 
    Home = 1, 
    Work = 2, 
    Mobile = 3, 
    Other = 4 
} 

I wtedy jestem wystające te typy do następujących modeli:

public class UserProfileModel 
{ 
    public Guid Id { get; set; } 
    public virtual string Name { get; set; } 
    public IEnumerable<UserProfilePhoneModel> Phones { get; set; } 
} 

public class UserProfilePhoneModel 
{ 
    public UserProfilePhoneTypeModel Type { get; set; } 
    public string Number { get; set; }   
} 

public enum UserProfilePhoneTypeModel 
{ 
    Home = 1, 
    Work = 2, 
    Mobile = 3, 
    Other = 4 
} 

Potem ustawienia mojego mapowania tak:

Mapper.CreateMap<PhoneType, UserProfilePhoneTypeModel>(); 
Mapper.CreateMap<UserProfilePhone, UserProfilePhoneModel>(); 
Mapper.CreateMap<UserProfile, UserProfileModel>(); 

I wreszcie wykonuję swoją projekcję:

var result = dbContext.UserProfiles.Project().To<UserProfileModel>(); 

Kiedy to zrobić, pojawia się następujący wyjątek:

AutoMapper.AutoMapperMappingException: Nie można utworzyć mapę ekspresji z MyNamespace.PhoneType do MyNamespace.Models.UserProfilePhoneTypeModel Nie można utworzyć mapę wyrażenie z MyNamespace.PhoneType do MyNamespace.Models.UserProfilePhoneTypeModel Wynik stackTrace:
na System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func 2 valueFactory) ...

Próbowałem tworzyć jawne odwzorowania, ale wydają się one ignorowane. Co ja tu robię źle?

+0

możliwe duplikat [Automapper wyliczenia wyliczają klasa] (http://stackoverflow.com/questions/24453398/automapper-enum -to-enumeration-class) –

Odpowiedz

10

Jak zwykle, wymyśliłem odpowiedź prawie tak szybko, jak napisałem pytanie.

Modyfikowanie utworzyć linię mapę, aby zapewnić wyraźny obsady wystarczyły:

Mapper.CreateMap<UserProfilePhone, UserProfilePhoneModel>() 
    .ForMember(m => m.Type, opt => opt.MapFrom(t => (UserProfilePhoneTypeModel)t.Type)); 
+3

Mapowanie działa bez rzutowania, ale kończy się niepowodzeniem, gdy próbujesz wyświetlić jedno wyliczenie do drugiego. Czy jest błąd Automappera? – vk5880

+0

Zdecydowanie wygląda na błąd. – RMD

Powiązane problemy