VirtualNomad
VirtualNomad

Reputation: 83

Problems while mapping using automapper C#

Have to transfer data from old database to new with some changes in the field. Tried using AutoMapper for the same. Below is the code

var crudCommonV1 = new HAES.EDMS.DAL.CRUD.CRUDCommon<HAES.EDMS.DAL.Address>();
var addressV1List = crudCommonV1.GetAll();
var config = new MapperConfiguration(cfg => {
    cfg.AllowNullCollections = true;
    cfg.AllowNullDestinationValues = true;
    cfg.CreateMap<HAES.EDMS.DAL.Address, HAES.EDMS.V2.DAL.Address>()
        .ForMember(dest => dest.ForeignAddress, act => act.MapFrom(src => src.OverrideAddress));
});
//automapper
IMapper mapper = config.CreateMapper();
var addressListMappedFromV2ToV1 = mapper.Map<IEnumerable<HAES.EDMS.DAL.Address>, IEnumerable<HAES.EDMS.V2.DAL.Address>>(addressV1List);

But it throws an exception

AutoMapper.AutoMapperMappingException

HResult=0x80131500

Message=Error mapping types.

Source=

StackTrace:

Inner Exception 1:

AutoMapperMappingException: Error mapping types.

Inner Exception 2:

AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Any idea what am doing wrong here?

EDIT :

Models : HAES.EDMS.DAL.V2.Address

public System.Guid Id { get; set; }
public System.Guid CollegeId { get; set; }
public short TypeId { get; set; }
public Nullable<System.Guid> PersonId { get; set; }
public string Pincode { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ForeignAddress { get; set; }
public System.DateTime CreatedOn { get; set; }
public string CreatedBy { get; set; }
public System.DateTime UpdatedOn { get; set; }
public string UpdatedBy { get; set; }

HAES.EDMS.DAL.Address

public System.Guid Id { get; set; }
public short TypeId { get; set; }
public System.Guid PersonId { get; set; }
public string Pincode { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string OverrideAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Remark { get; set; }
public System.DateTime CreatedOn { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> UpdatedOn { get; set; }
public string UpdatedBy { get; set; }

Upvotes: 2

Views: 1476

Answers (1)

kd345205
kd345205

Reputation: 319

I think Peter is correct. AutoMapper needs a custom map because of the type mismatch on "UpdatedOn". Change your createmap line to something like this...

cfg.CreateMap<HAES.EDMS.DAL.Address, HAES.EDMS.V2.DAL.Address>()
                    .ForMember(dest => dest.ForeignAddress, act => act.MapFrom(src => src.OverrideAddress))
                    .ForMember(m => m.UpdatedOn, o => o.MapFrom(f => f.UpdatedOn ?? DateTime.Today));

You can change the default behavior but you cant really just use .value on the property in case it's null.

Upvotes: 1

Related Questions