我使用 Autofac 和 Automapper,我有以下域类:
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 partial class Carreras
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Carreras()
{
Alumnos = new HashSet<Alumnos>();
}
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Descripcion { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Alumnos> Alumnos { get; set; }
}
public partial class Turnos
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Turnos()
{
Alumnos = new HashSet<Alumnos>();
}
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Descripcion { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Alumnos> Alumnos { get; set; }
}
这是我的模型:
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
}
public class CarreraModel
{
#region Fields
public int Id { get; set; }
public string Descripcion { get; set; }
#endregion
}
public class TurnoModel
{
#region Fields
public int Id { get; set; }
public string Descripcion { get; set; }
#endregion
}
我使用以下配置文件:
public class AlumnoProfile : Profile
{
public AlumnoProfile()
{
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 => src.Turnos))
.ForMember(d => d.Carrera, s => s.MapFrom(src => src.Carreras));
CreateMap<AlumnoModel, Alumnos>()
.ForMember(d => d.Id, s => s.Ignore())
.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));
}
}
public class CarreraProfile : Profile
{
public CarreraProfile()
{
CreateMap<Carreras, CarreraModel>()
.ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
.ForMember(d => d.Descripcion, s => s.MapFrom(src => src.Descripcion))
.ForAllOtherMembers(d => d.Ignore());
CreateMap<CarreraModel, Carreras>()
.ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
.ForAllOtherMembers(opts => opts.Ignore());
}
}
public class TurnoProfile : Profile
{
public TurnoProfile()
{
CreateMap<TurnoModel, Turnos>()
.ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
.ForAllOtherMembers(d => d.Ignore());
CreateMap<Turnos, TurnoModel>()
.ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
.ForMember(d => d.Descripcion, s => s.MapFrom(src => src.Descripcion))
.ForAllOtherMembers(d => d.Ignore());
}
}
这就是我使用 AutoFac 的方式:
builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile));
builder.Register(c => new MapperConfiguration
(cfg =>
{
foreach (var profile in c.Resolve<IEnumerable<Profile>>())
{
cfg.AddProfile(profile);
}
}
)).AsSelf().SingleInstance();
builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();
当我尝试从模型映射到域类时遇到问题(保存数据)
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
==============================================================
AlumnoModel -> Alumnos (Destination member list)
Prog_II.Data.Model.AlumnoModel -> Prog_II.Data.Domain.Alumnos (Destination member list)
Unmapped properties:
Carreras
Turnos
我尝试使用Memberlist.Source
and 对于我所做的每个属性:
.ForSourceMember(d => d.property, opt => opt.Ignore())
我尝试使用.ForAllOtherMembers(opt => opt.Ignore())
它起作用的唯一方法是如果我调用其他 2 个模型的 Mapper.Map,例如,如果我想映射AlumnoModel
到Alumnos
:
public Alumnos FromModelToDomain_Alumnos(AlumnoModel alumno)
{
return _Mapper.Map<Alumnos>(alumno);
}
但为了工作,我必须添加以下几行:
public Alumnos FromModelToDomain_Alumnos(AlumnoModel alumno)
{
FromModelToDomain_Turno(alumno.Turno);
FromModelToDomain_Carrera(alumno.Carrera);
return _Mapper.Map<Alumnos>(alumno);
}
public Turnos FromModelToDomain_Turno(TurnoModel turno)
{
return _Mapper.Map<Turnos>(turno);
}
public Carreras FromModelToDomain_Carrera(CarreraModel carrera)
{
return _Mapper.Map<Carreras>(carrera);
}
这是有道理的,因为我必须映射它们。
当我这样做时,它又引发了一个错误:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
============================================================
TurnoModel -> Turnos (Destination member list)
Prog_II.Data.Model.TurnoModel -> Prog_II.Data.Domain.Turnos (Destination member list)
Unmapped properties:
Alumnos
发生这种情况是因为表(EF 5)之间存在 1 到 n 的关系,因此Turnos
具有(域类)的 HashSet Alumnos
,但我希望忽略此属性,因为我的模型中没有它。