0

AutoMapper 可以映射共享属性吗?

实体

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

查看模型

public class ViewModel
{
    public string SomeProperty {get; set;}
    ...
    ...
    ...
}
4

2 回答 2

1

Although I have not used AutoMapper yet, I see no reason why you would not be able to achieve what you are looking for. Based on the project's documentation on Projection, you can write a projector:

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

// Perform mapping
ViewModel form = Mapper.Map<Entity, ViewModel>(entity);
于 2012-05-09T20:20:18.097 回答
0

你应该使用这样的代码:

Mapper.CreateMap<Entity, ViewModel>()
    .ForMember(
        dest => dest.SomeProperty,
        opt => opt.MapFrom(src => Entity.SomeProperty));
于 2013-02-04T00:49:08.987 回答