AutoMapper.Mapper nie zawiera definicji CreateMap
To może być podstawowe pytanie, ale zastanawiam się, czy nie dostanę Automapp.Mapper.Metoda CreateMap.
Czy używam niewłaściwej referencji/pakietu AutoMapper? Dzięki
3 answers
Statyczna Wersja metody CreateMap
została wycofana w wersji 4.2, a następnie usunięta z API w wersji 5.0. Jimmy Bogard opowiada o tym bardziej szczegółowo w ten wpis na blogu .
Nowa technika mapowania jest niestatyczna, jak to (kod pochodzi z posta):
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<Source, Dest>();
});
IMapper mapper = config.CreateMapper();
var source = new Source();
var dest = mapper.Map<Source, Dest>(source);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-25 23:05:02
Oto jak użyłem Automappera w moim kodzie.
Krok 1: pobranie AutoMapper poprzez nuget-packages .
Wersja to
<package id="AutoMapper" version="6.1.1" targetFramework="net452" />
Krok 1: utworzona Dto klasa
public class NotificationDTO
{
public DateTime DateTime { get; private set; }
public NotificationType Type { get; private set; }
public DateTime? OriginalDateTime { get; private set; }
public string OriginalVenue { get; private set; }
public ConcertDTO Concert { get; set; }
}
public class ConcertDTO
{
public int Id { get; set; }
public bool IsCancelled { get; private set; }
public DateTime DateTime { get; set; }
public string Venue { get; set; }
}
Krok 2: Utworzono klasę AutoMapperProfile , która dziedziczy z Profile
using AutoMapper;
using ConcertHub.DTOs;
namespace ConcertHub.Models
{
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<Concert, ConcertDTO>();
CreateMap<Notification, NotificationDTO>();
}
}
}
Krok 3: zarejestrowany AutoMapperProfile w metodzie Application Start W metodzie Global.asax Plik
protected void Application_Start()
{
AutoMapper.Mapper.Initialize(cfg => cfg.AddProfile<AutoMapperProfile>());
}
Wreszcie Magia fragment kodu w kontrolerze Api
public IEnumerable<NotificationDTO> GetNewNotification()
{
var userId = User.Identity.GetUserId();
var notifications = _dbContext.UserNotifications
.Where(un => un.UserId == userId && !un.IsRead)
.Select(un => un.Notification)
.Include(n => n.Concert)
.ProjectTo<NotificationDTO>()//use Automapper.QueryableExtension namespace
.ToList();
return notifications;
}
Mam nadzieję, że to pomoże .Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-31 04:35:41
Oto Jak to teraz działa:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<SupervisorEmployee, SupervisorViewModel>()
.ForMember
(dst => dst.Name, src => src.MapFrom<string>(e => SupervisorViewModel.MapName(e)))
.ForMember
(dst => dst.OfficePhone, src => src.MapFrom<string>(e => e.OfficePhone.FormatPhone(e.OfficePhoneIsForeign)))
.ForMember
(dst => dst.HomePhone, src => src.MapFrom<string>(e => e.HomePhone.FormatPhone(e.HomePhoneIsForeign)))
.ForMember
(dst => dst.MobilePhone, src => src.MapFrom<string>(e => e.MobilePhone.FormatPhone(e.MobilePhoneIsForeign)));
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-17 20:35:53