Created
November 26, 2018 13:51
-
-
Save matt-lethargic/735b2edf44b687f8ce5e490484ddb296 to your computer and use it in GitHub Desktop.
Auto Mapper Null Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
var mappingConfig = new MapperConfiguration(mc => { mc.AddProfile(new MappingProfile()); }); | |
IMapper mapper = mappingConfig.CreateMapper(); | |
var item = new Item | |
{ | |
CurrencyId = 123, | |
Id = 543, | |
Location = null, | |
Status = 99 | |
}; | |
var itemDto = new ItemDto | |
{ | |
CurrencyId = 123, | |
Id = 543, | |
Location = null, | |
Status = 99 | |
}; | |
var resultDto = mapper.Map<ItemDto>(item); | |
var resultPoco = mapper.Map<Item>(itemDto); | |
} | |
} | |
public class MappingProfile : Profile | |
{ | |
public MappingProfile() | |
{ | |
CreateMap<ItemDto, Item>() | |
.ForMember(dest => dest.Status, opts => opts.MapFrom(src => src.Status)) | |
.ReverseMap(); | |
CreateMap<LocationDto, Location>() | |
.ForMember(dest => dest.IsPrivate, opts => opts.MapFrom(src => src.IsUserDefined)) | |
.ReverseMap(); | |
} | |
} | |
public class Item | |
{ | |
public long Id { get; set; } | |
public int? CurrencyId { get; set; } | |
public Location Location { get; set; } | |
public int Status { get; set; } | |
} | |
public class Location | |
{ | |
public long Id { get; set; } | |
public string Address { get; set; } | |
public bool IsPrivate { get; set; } = false; | |
[NotMapped] | |
public float Latitude { get; set; } | |
[NotMapped] | |
public float Longitude { get; set; } | |
} | |
public class ItemDto | |
{ | |
public long Id { get; set; } | |
public int? CurrencyId { get; set; } | |
public LocationDto Location { get; set; } | |
public int Status { get; set; } | |
} | |
public class LocationDto | |
{ | |
public long Id { get; set; } | |
public string Address { get; set; } | |
public float Latitude { get; set; } | |
public float Longitude { get; set; } | |
public bool IsUserDefined { get; set; } | |
public DateTimeOffset UpdatedAt { get; set; } | |
public DateTimeOffset CreatedAt { get; set; } | |
public bool Disabled { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment