在共享属性上使用AutoMapper?

时间:2012-05-09 19:57:19

标签: automapper

AutoMapper可以映射共享属性吗?

实体

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

查看模型

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

你应该使用这样的代码:

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