I have the following entities:
public class ContactDetailsJson
{
public string Zip { get; set; }
public string Phone { get; set; }
public string AreaCode { get; set; }
public DateTime UpdatedDate { get; set; }
public string SeoContactUrl { get; set; }
public string State { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string CompanyName { get; set; }
public string ContactUrl { get; set; }
public string Country { get; set; }
public bool Owned { get; set; }
public string City { get; set; }
public string Title { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string CompanyId { get; set; }
public string ContactId { get; set; }
}
public class ExternalContactSearchResultsViewModel
{
public int DisplayedPageNumber { get; set; }
public int TotalResultsCount { get; set; }
public int PageSize { get; set; }
public IList<ContactResultViewModel> Results { get; set; }
public class ContactResultViewModel
{
public string ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Headline { get; set; }
public string Company { get; set; }
public string PublicUrl { get; set; }
public bool HasAccess { get; set; }
public DateTime LastUpdatedDate { get; set; }
}
}
To support the conversion, I have the following mappings created, which I have verified are being run:
Mapper.CreateMap<ContactDetailsJson, ExternalContactSearchResultsViewModel.ContactResultViewModel>()
.ForMember(dest => dest.Company, opt => opt.MapFrom(src => src.CompanyName))
.ForMember(dest => dest.ContactId, opt => opt.MapFrom(src => src.ContactId))
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName))
.ForMember(dest => dest.HasAccess, opt => opt.MapFrom(src => src.Owned))
.ForMember(dest => dest.Headline, opt => opt.MapFrom(src => src.Title))
.ForMember(dest => dest.LastUpdatedDate, opt => opt.MapFrom(src => src.UpdatedDate))
.ForMember(dest => dest.PublicUrl, opt => opt.Ignore());
Unfortunately, this mapping results in all properties being null (or default values), as can be seen from the following unit test
[TestMethod]
public void Automapper_Contact_Details_Json_Can_Be_Mapped()
{
// Setup
EntityMapLoader.LoadEntityMappings();
DateTime testDate = DateTime.Now;
var json = new ContactDetailsJson
{
CompanyName = "company",
ContactId = "12345",
FirstName = "first",
LastName = "last",
Owned = true,
Title = "title",
UpdatedDate = testDate
};
// Act
var result = Mapper.Map<ContactDetailsJson, ExternalContactSearchResultsViewModel.ContactResultViewModel>(json);
// Verify
Assert.IsNotNull(result, "result was null");
Assert.AreEqual("company", result.Company);
Assert.AreEqual("12345", result.ContactId);
Assert.AreEqual("first", result.FirstName);
Assert.AreEqual("last", result.LastName);
Assert.AreEqual(true, result.HasAccess);
Assert.AreEqual("title", result.Headline);
Assert.AreEqual(testDate, result.LastUpdatedDate);
}
I can’t figure out what is wrong. Does anyone else see anything?
I tried this and it works for me, I think its something wrong in way you are loading mapping or check if you are using latest version of automapper