2012-05-09 52 views
0

AutoMapper可以映射共享屬性嗎?在共享屬性上使用AutoMapper?

實體

public class Entity 
{ 
    public static string SomeProperty {get; set;} 
    ... 
    ... 
    ... 
} 

視圖模型

public class ViewModel 
{ 
    public string SomeProperty {get; set;} 
    ... 
    ... 
    ... 
} 
+0

如何這些屬性共享?他們生活在兩個不相關的類中。另外,這是[AutoMapper](http://automapper.org/)你在說什麼? – Attila

+0

是的,我正在使用該Automapper,並且我想將該實體映射到視圖模型。 – Sam

回答

1

雖然我還沒有使用AutoMapper呢,我看不出有任何理由,你爲什麼就不能達到你在找什麼。根據該項目的documentation on Projection,你可以寫一個投影機:

Mapper.CreateMap<Entity, ViewModel>() 
    .ForMember(dest => dest.SomeProperty, opt => opt.MapFrom(src => src.SomeProperty)); 

// Perform mapping 
ViewModel form = Mapper.Map<Entity, ViewModel>(entity); 
0

你應該使用這樣的代碼:

Mapper.CreateMap<Entity, ViewModel>() 
    .ForMember(
     dest => dest.SomeProperty, 
     opt => opt.MapFrom(src => Entity.SomeProperty));