自动映射器忽略复杂属性

时间:2017-11-13 15:00:31

标签: c# automapper

我使用Automapper v6.2和配置文件以及Autofac来注册这些配置文件,我的Domain类具有我的模型没有的属性,因为我不需要它们(例如Id或其他属性只需要保存在我的分贝)

例如:

public class TurnoProfile : Profile
{
    public TurnoProfile()
    {
        CreateMap<Turnos, TurnoModel>()
            .ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
            .ForMember(d => d.Descripcion, s => s.MapFrom(src => src.Descripcion));

        CreateMap<TurnoModel, Turnos>()
            .ForMember(d => d.Id, s => s.MapFrom(src => src.Id))
            .ForMember(d => d.Descripcion, s => s.MapFrom(src => src.Descripcion))
            .ForMember(d => d.Alumnos, s => s.Ignore());
    }
}

域对象(EF 5)

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 TurnoModel
{
    #region Fields
    public int Id { get; set; }
    public string Descripcion { get; set; } 
    #endregion
}

正如您所看到的,我不希望Alumnos被映射,这是一对多关系,我想忽略哪个Alumnos有某些Turnos

但我有以下错误:

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

0 个答案:

没有答案