在nullables之间映射异常?

时间:2015-09-28 13:33:08

标签: c# automapper

我遇到了这个例外:

  

缺少类型映射配置或不支持的映射。

     

映射类型:   FollowUpActivityDTO - > Nullable 1 LEST.Model.FollowUpActivityDTO -> System.Nullable 1 [[System.DateTime,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]

     

目的地路径:   List`1 [0]

     

来源价值:

     

class FollowUpActivityDTO {     Id:75e83860-65e4-11e5-9a90-382c4ab9e433     时间戳:28/09/2015 11:28:55     StartTimestamp:28/09/2015 15:26:00     DueTimestamp:28/09/2015 15:26:00     ClosingTimestamp:28/09/2015 15:26:00     问题:elasticsearch - copia - copia.jar     评论:存档     状态:开放     BacklogStatus:未知   }

{Trying to map FollowUpActivityDTO to Nullable`1.}

我表演的时候会加注:

return AutoMapper.Mapper.Map<List<LEST.Model.FollowUpActivityDTO>, List<Domain.FollowUpActivity>>(dtos);

这是目的地类:

public class FollowUpActivity
{
    private String id;              // Generated on server
    private DateTime? creationDate;
    private DateTime? startDate;
    private DateTime? dueDate;
    private DateTime? closingDate;
    private String matter;
    private String comment;
    private FollowUpActivityStatus status;
    private FollowUpActivityBacklogStatus backlogStatus;

    private List<MetaInfoValue> metainfos;
    private List<MetaResource> resources;

这是源类:

 public class FollowUpActivityDTO
{
    public FollowUpActivityDTO();

    public string BacklogStatus { get; set; }
    public string ChannelId { get; set; }
    public DateTime? ClosingTimestamp { get; set; }
    public string Comment { get; set; }
    public DateTime? DueTimestamp { get; set; }
    public string Id { get; set; }
    public string Matter { get; set; }
    public DateTime? StartTimestamp { get; set; }
    public string Status { get; set; }
    public DateTime? Timestamp { get; set; }

映射配置文件:

AutoMapper.Mapper.CreateMap<LEST.Model.FollowUpActivityDTO, Domain.FollowUpActivity>()
            .ForMember(dst => dst.CreationDate, opts => opts.Ignore()) //opts.MapFrom(s => s.Timestamp.HasValue ? s.Timestamp.Value : DateTime.MinValue))
            .ForMember(dst => dst.StartDate, opts => opts.Ignore()) //opts.MapFrom(s => s.StartTimestamp.HasValue ? s.StartTimestamp.Value : DateTime.MinValue))
            .ForMember(dst => dst.DueDate, opts => opts.Ignore()) //opts.MapFrom(s => s.DueTimestamp.HasValue ? s.DueTimestamp.Value : DateTime.MinValue))
            .ForMember(dst => dst.ClosingDate, opts => opts.Ignore()) //opts.MapFrom(s => s.ClosingTimestamp.HasValue ? s.ClosingTimestamp.Value : DateTime.MinValue));
            .ForMember(dst => dst.Status, opts => opts.UseValue<Domain.FollowUpActivityStatus>(Domain.FollowUpActivityStatus.Open))
            .ForMember(dst => dst.BacklogStatus, opts => opts.UseValue<Domain.FollowUpActivityBacklogStatus>(Domain.FollowUpActivityBacklogStatus.Work));

1 个答案:

答案 0 :(得分:1)

我已经创建了一个用于测试此映射的测试:

static

看看:

namespace Tests
{
[TestFixture]
public class Mapping
{

    [SetUp]
    public void initialize()
    {
        Core.Mappings.AutoMapperConfiguration.Configure();
    }

    [Test]
    public void configutation()
    {
        AutoMapper.Mapper.AssertConfigurationIsValid<Core.Mappings.Profiles.FollowUpActivityProfile>();
    }

    [Test]
    public void followUpActivityDTOToDOMAIN()
    {
        LEST.Model.FollowUpActivityDTO dto = new LEST.Model.FollowUpActivityDTO()
        {
            Id = new Guid().ToString(),
            Timestamp = DateTime.UtcNow,
            StartTimestamp = DateTime.UtcNow,
            DueTimestamp = DateTime.UtcNow,
            ClosingTimestamp = DateTime.UtcNow,
            Matter = "Matter",
            Comment = "Comment",
            Status = "open",
            BacklogStatus = "work",
            Metainfos = new System.Collections.Generic.List<LEST.Model.MetaInfoValueDTO>()
        };

        Domain.FollowUpActivity domain = new Domain.FollowUpActivity();
        AutoMapper.Mapper.Map<LEST.Model.FollowUpActivityDTO, Domain.FollowUpActivity>(dto, domain);

        domain.Should().NotBeNull();
        domain.Id.Should().Be(dto.Id);
    }
}
}

现在我工作了。但是,如果我编码如下,它会崩溃:

Domain.FollowUpActivity domain = new Domain.FollowUpActivity();
AutoMapper.Mapper.Map<LEST.Model.FollowUpActivityDTO, Domain.FollowUpActivity>(dto, domain);

我发现问题在于我的Destination类的构造函数:

Domain.FollowUpActivity domain = AutoMapper.Mapper.Map<LEST.Model.FollowUpActivityDTO, Domain.FollowUpActivity>(dto);

但是,我不知道为什么它会使用第二种方法崩溃...有人知道为什么吗? 谢谢大家。