0

Im using Autofac and Automapper, I have the following Domain classes:

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; }
}

And this are my Models:

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
}

And Im using the following Profiles:

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());



    }
}

And this is how Im using 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();

And Im having and issue when Im trying to map from the model to the domain class (Save data)

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

I tried using Memberlist.Source and for each property I did:

.ForSourceMember(d => d.property, opt => opt.Ignore())

I tried using .ForAllOtherMembers(opt => opt.Ignore())

The only way it works is if I call the Mapper.Map of the other 2 Models, for example, if I want to map AlumnoModel to Alumnos:

    public Alumnos FromModelToDomain_Alumnos(AlumnoModel alumno)
    {
        return _Mapper.Map<Alumnos>(alumno);
    }

but in order to work I have to add the following lines:

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);
}

Which makes sense as I have to map them.

When I do this It throws one more error:

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

And this happens because Turnos have a HashSet of Alumnos (Domain class) because of a 1 to n relationship between the tables (EF 5), but I want this property to be ignored as I dont have it in my model.

Nickso
  • 785
  • 1
  • 10
  • 32

1 Answers1

0

Your mapping configuration looks a bit odd to me - you're doing explicit mapping for single properties whose names match, but ignoring everything else? That would tell AutoMapper "only do these two properties but nothing else" which seems odd, why even use AutoMapper?

It looks like you might want to check out reverse mapping:

http://docs.automapper.org/en/stable/Reverse-Mapping-and-Unflattening.html

That may be closer to what you're trying to achieve. Ignore means "don't map this field", which I don't think you want?

Jimmy Bogard
  • 26,045
  • 5
  • 74
  • 69