Automapper - 将实体类型映射到嵌套视图模型
Automapper - Mapping entity types to nested view models
假设我有一个这样的 EF 实体:
public class Person
{
public string Name { get; set; }
public string Psuedonym { get; set; }
public bool HasPsuedonym { get; set; }
}
我想从这个实体映射到如下所示的视图模型:
public class PersonViewModel
{
public string Name { get; set; }
public PsuedonymViewModel { get; set; }
}
其中包含另一个视图模型:
public class PsuedonymViewModel
{
public string Psuedonym { get; set; }
public bool HasPsuedonym { get; set; }
}
我需要设置什么映射?
我有一个创建以下映射的通用映射设置:
Person > PersonViewModel - memberlist.dest
PersonViewModel > Person - memberlist.source
PsuedonymViewModel > Person - memberlist.dest
Person > PsuedonymViewModel - memberlist.source
通用映射仅映射源或目标中存在的属性,具体取决于映射的方式,这样我就不必手动为不存在的属性添加一大堆忽略项匹配。
当我有这个设置时,automapper 说:
AutoMapper.AutoMapperConfigurationException: The following property on MyApplication.PsuedonymViewModel cannot be mapped:
Psuedonym
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type MyApplication.PsuedonymViewModel.
Context:
Mapping to property Psuedonym from System.String to MyApplication.PsuedonymViewModel
我理解此消息是因为当尝试从 Person
实体映射到 PsuedonymViewModel
时,没有从 string
到 Psuedonym
的映射。我可以用这样的手动映射来解决这个问题:
CreateMap<string, PsuedonymViewModel>()
.ForMember(x => x.HasPsuedonym, opt => opt.Ignore())
.ForMember(x => x.Psuedonym, opt => opt.MapFrom(y => y));
虽然这映射了 Psuedonym
属性 我必须忽略 HasPsuedonym
属性 这不是我想要的,但因为它是从 string
没有其他我可以使用的东西了。
感觉我在这里犯了一些基本错误,但我想不通,任何想法都非常感谢。
当您使用 Person
构建 PsuedonymViewModel
以及 PersonViewModel
时,您必须构建两个映射,并且在根对象映射中只需指向构建 属性 来自同一个源对象:
cfg.CreateMap<Person, PsuedonymViewModel>();
cfg.CreateMap<Person, PersonViewModel>()
.ForMember(x=>x.Psuedonym, m=>m.MapFrom(p=>p));
假设我有一个这样的 EF 实体:
public class Person
{
public string Name { get; set; }
public string Psuedonym { get; set; }
public bool HasPsuedonym { get; set; }
}
我想从这个实体映射到如下所示的视图模型:
public class PersonViewModel
{
public string Name { get; set; }
public PsuedonymViewModel { get; set; }
}
其中包含另一个视图模型:
public class PsuedonymViewModel
{
public string Psuedonym { get; set; }
public bool HasPsuedonym { get; set; }
}
我需要设置什么映射?
我有一个创建以下映射的通用映射设置:
Person > PersonViewModel - memberlist.dest
PersonViewModel > Person - memberlist.source
PsuedonymViewModel > Person - memberlist.dest
Person > PsuedonymViewModel - memberlist.source
通用映射仅映射源或目标中存在的属性,具体取决于映射的方式,这样我就不必手动为不存在的属性添加一大堆忽略项匹配。
当我有这个设置时,automapper 说:
AutoMapper.AutoMapperConfigurationException: The following property on MyApplication.PsuedonymViewModel cannot be mapped:
Psuedonym
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type MyApplication.PsuedonymViewModel.
Context:
Mapping to property Psuedonym from System.String to MyApplication.PsuedonymViewModel
我理解此消息是因为当尝试从 Person
实体映射到 PsuedonymViewModel
时,没有从 string
到 Psuedonym
的映射。我可以用这样的手动映射来解决这个问题:
CreateMap<string, PsuedonymViewModel>()
.ForMember(x => x.HasPsuedonym, opt => opt.Ignore())
.ForMember(x => x.Psuedonym, opt => opt.MapFrom(y => y));
虽然这映射了 Psuedonym
属性 我必须忽略 HasPsuedonym
属性 这不是我想要的,但因为它是从 string
没有其他我可以使用的东西了。
感觉我在这里犯了一些基本错误,但我想不通,任何想法都非常感谢。
当您使用 Person
构建 PsuedonymViewModel
以及 PersonViewModel
时,您必须构建两个映射,并且在根对象映射中只需指向构建 属性 来自同一个源对象:
cfg.CreateMap<Person, PsuedonymViewModel>();
cfg.CreateMap<Person, PersonViewModel>()
.ForMember(x=>x.Psuedonym, m=>m.MapFrom(p=>p));