[英]Automapper mapping issues child object properties
我在这里遵循有关此主题的不同问题/ 答案 ,我在两天前开始使用AutoMapper时遇到了困难。
这是我的模型(正面):
public class AlumnoModel
{
#region Fields
public int Id { get; set; }
public string Nombre { get; set; }
public string Apellido { get; set; }
public string Legajo { get; set; }
public int Dni { get; set; }
public TurnoModel Turno { get; set; }
public CarreraModel Carrera { get; set; }
#endregion
}
这是我的域(由EF首先使用本地数据库执行代码创建)
public partial class Alumnos
{
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Nombre { get; set; }
[Required]
[StringLength(50)]
public string Apellido { get; set; }
[Required]
[StringLength(50)]
public string Legajo { get; set; }
public int Dni { get; set; }
public int Carrera { get; set; }
public int Turno { get; set; }
public virtual Carreras Carreras { get; set; }
public virtual Turnos Turnos { get; set; }
}
我就是这样尝试映射它们的:
public Alumnos FromModelToDomain_Alumnos(AlumnoModel alumno)
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Alumnos, AlumnoModel>()
.ForMember(d => d.Nombre, s => s.MapFrom(src => src.Nombre))
.ForMember(d => d.Apellido, s => s.MapFrom(src => src.Apellido))
.ForMember(d => d.Legajo, s => s.MapFrom(src => src.Legajo))
.ForMember(d => d.Dni, s => s.MapFrom(src => src.Dni))
.ForMember(d => d.Turno, s => s.MapFrom(src => new Carreras
{
Id = src.Turnos.Id,
Descripcion = src.Turnos.Descripcion
}))
.ForMember(d => d.Carrera, s => s.MapFrom(src => new Turnos
{
Id = src.Carreras.Id,
Descripcion = src.Carreras.Descripcion
}))
.ForMember(d => d.Carrera.Id, s => s.MapFrom(src => src.Carrera))
.ForMember(d => d.Turno.Id, s => s.MapFrom(src => src.Turno));
});
return Mapper.Map<Alumnos>(alumno);
}
这是异常的消息:
Expression 'd => d.Carrera.Id' must resolve to top-level member and not any child object's properties. Use a custom resolver on the child type or the AfterMap option instead.
Parameter name: lambdaExpression.
我将阅读有关AfterMap
更多信息,但我认为创建一个新的child实例并填充其属性足以进行映射。
编辑:
好的,我对mapper方法进行了一些更改,如下所示:
public Alumnos FromModelToDomain_Alumnos(AlumnoModel alumno)
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<AlumnoModel, Alumnos>()
.ForMember(d => d.Nombre, s => s.MapFrom(src => src.Nombre))
.ForMember(d => d.Apellido, s => s.MapFrom(src => src.Apellido))
.ForMember(d => d.Legajo, s => s.MapFrom(src => src.Legajo))
.ForMember(d => d.Dni, s => s.MapFrom(src => src.Dni))
.ForMember(d => d.Turnos, s => s.MapFrom(src => new Carreras
{
Id = src.Turno.Id,
}))
.ForMember(d => d.Carreras, s => s.MapFrom(src => new Turnos
{
Id = src.Carrera.Id,
}))
.ForMember(d => d.Carrera, s => s.MapFrom(src => src.Carrera.Id))
.ForMember(d => d.Turno, s => s.MapFrom(src => src.Turno.Id));
});
return Mapper.Map<Alumnos>(alumno);
}
如果我从那里拿走new
的东西,它可以工作,但是那些属性当然是空的。
异常消息:
Error mapping types.
Mapping types:
AlumnoModel -> Alumnos
Prog_II.Models.AlumnoModel -> Repository.Domain.Alumnos
Type Map configuration:
AlumnoModel -> Alumnos
Prog_II.Models.AlumnoModel -> Repository.Domain.Alumnos
Property:
Turnos
好的,我找到了一个解决方案,也许有点笨重,但它确实完成了工作:
public AlumnoModel FromDomainToModel_Alumnos(Alumnos alumno)
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Turnos, TurnoModel>()
.ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
.ForMember(d => d.Descripcion, s => s.MapFrom(src => src.Descripcion));
cfg.CreateMap<Carreras, CarreraModel>()
.ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
.ForMember(d => d.Descripcion, s => s.MapFrom(src => src.Descripcion));
cfg.CreateMap<AlumnoModel, Alumnos>()
.ForMember(d => d.Nombre, s => s.MapFrom(src => src.Nombre))
.ForMember(d => d.Apellido, s => s.MapFrom(src => src.Apellido))
.ForMember(d => d.Legajo, s => s.MapFrom(src => src.Legajo))
.ForMember(d => d.Dni, s => s.MapFrom(src => src.Dni))
.ForMember(d => d.Carrera, s => s.MapFrom(src => src.Carrera.Id))
.ForMember(d => d.Turno, s => s.MapFrom(src => src.Turno.Id))
.ForMember(d => d.Turnos, s => s.MapFrom(src => src.Turno))
.ForMember(d => d.Carreras, s => s.MapFrom(src => src.Carrera));
});
return Mapper.Map<AlumnoModel>(alumno);
}
我不明白为什么初始化其他域不起作用。 在这里,我们告诉Automapper,每当您要映射Alumnos
并在其中找到Turnos
和Carreras
时,请使用上面的.CreateMap
进行解析。
您必须在Initialize内部(在将要映射的内容与内容之间的实例中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.