AutoMapper 可以映射共享属性吗?
实体
public class Entity
{
public static string SomeProperty {get; set;}
...
...
...
}
查看模型
public class ViewModel
{
public string SomeProperty {get; set;}
...
...
...
}
AutoMapper 可以映射共享属性吗?
实体
public class Entity
{
public static string SomeProperty {get; set;}
...
...
...
}
查看模型
public class ViewModel
{
public string SomeProperty {get; set;}
...
...
...
}
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);
你应该使用这样的代码:
Mapper.CreateMap<Entity, ViewModel>()
.ForMember(
dest => dest.SomeProperty,
opt => opt.MapFrom(src => Entity.SomeProperty));