I am trying to map a ViewModel to a model. I am new to Automapper and I find the documentation incomplete. After hours of search, stackoverflow seems to be the last stop.
This is what I am trying to map:
ViewModel
public class UpdatedCompleteLocation
{
public double? Longitude { get; set; }
public double? Latitude { get; set; }
public string? Address { get; set; }
public string? City { get; set; }
public string? CountryCode { get; set; }
public string? Region { get; set; }
public string? Country { get; set; }
}
To:
Model
public class EventLocation
{
public int Id { get; set; }
public string? EntityName { get; set; }
public string? City { get; set; }
public string? Region { get; set; }
public string? Address { get; set; }
public string? Country { get; set; }
public string? CountryCode { get; set; }
[Column(TypeName = "geometry (point)")]
public Point Location { get; set; }
}
The trick comes when I try to map Longitude, Latitude to X, Y in the Location
of type Point which is part of NetTopologySuite.Geometries
. Also as a precondition, the lat/long has to be not null.
This is what I came up with so far:
CreateMap<UpdatedCompleteLocation, EventLocation>()
.ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));
If you want to stick with Automapper, you must add a custom conversion for the Location
property. Using your existing CreateMap, you can extend it like:
CreateMap<UpdatedCompleteLocation, EventLocation>()
.ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null))
.ForMember(dest => dest.Location, opt => opt.MapFrom(src => new Point(src.Longitude, src.Latitude));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.