Basically, im trying to convert a VehicleDTO, which has some properties, to my Vehicle model, which has a lot of more properties, so when i map it, the unmapped properties are null, and when its time to update it, that causes a couple of errors, like some values that cant be null, or just updates the object but clears the previous values and set them to null.
Im using entity framework
/*AutoMapperProfile.cs*/
CreateMap<VehiculoDTO, Vehiculo>()
.ConvertUsing<MapeoVehiculosDTO_VehiculosTypeConverter>();
/*FuncionesMapeos.cs*/
public class MapeoVehiculosDTO_VehiculosTypeConverter : ITypeConverter<VehiculoDTO, Vehiculo> {
public Vehiculo Convert(VehiculoDTO source, Vehiculo destination, ResolutionContext context) {
destination = new();
if (source is not null) {
destination.Id = source.Id;
destination.Matricula = source.Matricula;
if (source.IdMarca is not null) {
destination.MarcaId = source.IdMarca;
}
if (source.IdModelo is not null) {
destination.ModeloId = source.IdModelo;
}
if (source.Id_TipoCombustible is not null) {
destination.TipoCombustibleId = source.Id_TipoCombustible;
}
destination.IdEmpresa = source.IdEmpresa;
}
return destination;
}
}
I tried to use functions like IgnoreAllUnmapped, but i always get the same result.
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.