空值之间的映射异常

本文关键字:异常 映射 之间 空值 | 更新日期: 2023-09-27 18:14:47

我得到了这个异常:

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

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

目的地路径:列表1 [0]

源值:

类FollowUpActivityDTO {Id: 75 e83860 - 65 - e4 - 11 - e5 - 9 -前- 382 c4ab9e433时间戳:28/09/2015 11:28:55StartTimestamp: 28/09/2015 15:26:00DueTimestamp: 28/09/2015 15:26:00ClosingTimestamp: 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));

空值之间的映射异常

我已经创建了一个测试来测试这个映射:

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);

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

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

我想问题是在我的目的地类的构造函数,然后:

namespace Domain
{
public enum FollowUpActivityStatus
{
    Open,
    Closed,
    Delegated
}
public enum FollowUpActivityBacklogStatus
{
    Work,
    Sprint        
}
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;
    #region Properties
    public String Id
    {
        get { return id; }
        set { id = value; }
    }
    public DateTime? CreationDate
    {
        get { return creationDate; }
        set { creationDate = value; }
    }
    public DateTime? StartDate
    {
        get { return startDate; }
        set { startDate = value; }
    }
    public DateTime? DueDate
    {
        get { return dueDate; }
        set { dueDate = value; }
    }
    public DateTime? ClosingDate
    {
        get { return closingDate; }
        set { closingDate = value; }
    }
    public FollowUpActivityStatus Status
    {
        get { return status; }
        set { status = value; }
    }
    public FollowUpActivityBacklogStatus BacklogStatus
    {
        get { return backlogStatus; }
        set { backlogStatus = value; }
    }
    public String Matter
    {
        get { return matter; }
        set { matter = value; }
    }
    public String Comment
    {
        get { return comment; }
        set { comment = value; }
    }
    public List<MetaInfoValue> Metainfos
    {
      get { return metainfos; }
      set { metainfos = value; }
    }
    public List<MetaResource> Resources
    {
        get { return resources; }
        set { resources = value; }
    }
    #endregion
    #region Constructors
    public FollowUpActivity()
        : this(null, null)
    {
    }
    public FollowUpActivity(String matter, String comment = null, params Domain.MetaInfoValue[] metainfos)
        : this(DateTime.Now, matter, comment, metainfos)
    {
    }
    public FollowUpActivity(DateTime creationDate, String matter, String comment = null, params Domain.MetaInfoValue[] metainfos)
        : this(creationDate, matter, new List<MetaResource>(), comment, metainfos)
    {
    }
    public FollowUpActivity(DateTime creationDate, String matter, List<MetaResource> resources, String comment = null, params Domain.MetaInfoValue[] metainfos)
        : this(creationDate, matter, FollowUpActivityBacklogStatus.Work, new List<MetaResource>(), comment, metainfos)
    {
    }
    public FollowUpActivity(DateTime creationDate, String matter, FollowUpActivityBacklogStatus backlogStatus, List<MetaResource> resources, String comment = null, params Domain.MetaInfoValue[] metainfos)
        : this(creationDate, null, null, null, matter, FollowUpActivityStatus.Open, backlogStatus, new List<MetaResource>(), comment, metainfos)
    {
    }
    public FollowUpActivity(DateTime? creationDate, DateTime? startDate, DateTime? dueDate, DateTime? closingDate, String matter, FollowUpActivityStatus status, FollowUpActivityBacklogStatus backlogStatus, List<MetaResource> resources, String comment = null, params Domain.MetaInfoValue[] metainfos)
    {
        this.id = String.Empty;
        this.creationDate = creationDate;
        this.startDate = startDate;
        this.dueDate = dueDate;
        this.closingDate = closingDate;
        this.matter = matter;
        this.comment = comment;
        this.status = status;
        this.backlogStatus = backlogStatus;
        this.metainfos = new List<MetaInfoValue>(metainfos);
        this.resources = resources;
    }
    #endregion
}
}

然而,我不知道为什么使用第二种方法会崩溃…有人知道为什么吗?