无法使Automapper忽略属性

时间:2018-09-21 16:02:15

标签: c# automapper

我对自动映射器进行了以下设置;

cfg.CreateMap<NewBuildBlockViewModel, NewBuildBlock>();
cfg.CreateMap<NewBuildBlockAreaViewModel, NewBuildBlockArea>()
                    .ForMember(x => x.CreatedBy, opt => opt.Ignore()).ForMember(x => x.Created, opt => opt.Ignore())
                    .ForMember(x => x.LastModifiedBy, opt => opt.Ignore()).ForMember(x => x.LastModified, opt => opt.Ignore());

因此对于第二个映射,我想忽略审核字段。 这些课程如下

public class NewBuildBlockViewModel
{
    public NewBuildBlockViewModel()
    {
        NewBuildBlockArea = new List<NewBuildBlockAreaViewModel>();
    }

    public int NewBuildBlockId { get; set; }
    public int ContractId { get; set; }
    public IList<NewBuildBlockAreaViewModel> NewBuildBlockArea { get; set; }

}

public class NewBuildBlockAreaViewModel
{
    public int NewBuildBlockAreaId { get; set; }
    public int NewBuildBlockId { get; set; }

    [MaxLength(255)]
    public string Name { get; set; }

    public bool IsPlot { get; set; }
    public bool IsDeletable { get; set; }
}

public class NewBuildBlock : IAuditCreated, IAuditLastModified
{
    [Key] public int NewBuildBlockId { get; set; }

    public int ContractId { get; set; }
    public DateTime Created { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? LastModified { get; set; }
    public string LastModifiedBy { get; set; }
    public List<NewBuildBlockArea> NewBuildBlockArea { get; set; }
}

public class NewBuildBlockArea : IAuditCreated, IAuditLastModified
{
    [Key] public int NewBuildBlockAreaId { get; set; }
    public int NewBuildBlockId { get; set; }

    [MaxLength(255)]
    public string Name { get; set; }

    public bool IsPlot { get; set; }
    public DateTime Created { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? LastModified { get; set; }
    public string LastModifiedBy { get; set; }
    public NewBuildBlock NewBuildBlock { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

如您所见,我正在映射带有嵌套列表的类 在映射期间,我希望忽略审核字段,但是对于列表,我发现它们被设置为默认值,因此Created显示最小日期01/01/0001,CreatedBy设置为null。 此代码有什么问题?我正在使用6.1.1版。

var originalNewBuild = this.SirUoW.NewBuildBlock.GetItem(                    
    x => x.BlockUPRN == blockUPRN && x.ContractId == contractId, x => 
    x.NewBuildBlockArea);

var newNewBuild = Mapper.Map(newBuildBlockViewModel, originalNewBuild);

0 个答案:

没有答案