[英]c# automapper map not null object to null
我想在 automapper 中始终设置我的 object 到 null 的值。
CreateMap<Activity, Activity>()
.ForMember(x => x.Category, null );
但是我收到了 null 参考异常
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=AutoMapper
StackTrace:
at AutoMapper.Configuration.MappingExpression`2.ForDestinationMember[TMember](MemberInfo destinationProperty, Action`1 memberOptions)
at Application.Core.MappingProfiles..ctor() in C:\Users\Bryan.Dellinger.Apps\Documents\code\EEMRewrite\Application\Core\MappingProfiles.cs:line 17
这是我的活动 class
public class Activity
{
public Guid Id { get; set; }
public string Title { get; set; }
public DateTime Start {get; set;}
public DateTime End { get; set; }
public string Description {get; set;}
public Guid CategoryId { get; set; }
public Category Category { get; set; }
}
Johnathan Barclay在评论中给出了正确的答案:
CreateMap<Activity, Activity>()
.ForMember(x => x.Category, opt => opt.MapFrom<Category>(_ => null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.