我有以下实体:
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; }
}
}
为了支持转换,我创建了以下映射,我已经验证了这些映射正在运行:
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());
不幸的是,这种映射导致所有属性都为null(或默认值),从以下单元测试中可以看出
[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);
}
我无法弄清楚出了什么问题。有没有人看到任何东西?
答案 0 :(得分:1)
我尝试了这个并且它对我有用,我认为你加载映射或检查你是否使用最新版本的automapper时出现了错误
[TestFixture]
public class UnitTest
{
[Test]
public void Automapper_Contact_Details_Json_Can_Be_Mapped()
{
// Setup
DateTime testDate = DateTime.Now;
var json = new ContactDetailsJson
{
CompanyName = "company",
ContactId = "12345",
FirstName = "first",
LastName = "last",
Owned = true,
Title = "title",
UpdatedDate = testDate
};
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());
// 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);
}
}