[英]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.