Vlad
Vlad

Reputation: 233

automapper Error mapping types

Everything was fine, until I added DataTime property to my Model and viewModel. All data is correct, but error occurs anyway. I think the error appears because of I added PublicationDate. As I understand, Automapper2.0 should work fine with dates,so I dunno why does it happen

Error occurs here:

Publication viewModelPublication = new Publication(Mapper.Map<PublicationViewModel, Publication>(viewModel));

My AutoMapperConfig:

 public class AutoMapperConfig
{
    public static void Initialize()
    {
        Mapper.Initialize(cfg =>
        {

            cfg.CreateMap<Author, AuthorViewModel>()
           .ForMember(a => a.AuthorId, opt => opt.MapFrom(a => a.AuthorId))
           .ForMember(a => a.AuthorName, opt => opt.MapFrom(a => a.AuthorName));

            cfg.CreateMap<AuthorViewModel, Author>()
          .ForMember(a => a.AuthorId, opt => opt.MapFrom(a => a.AuthorId))
          .ForMember(a => a.AuthorName, opt => opt.MapFrom(a => a.AuthorName));


            cfg.CreateMap<Publication, PublicationViewModel>()
         .ForMember(p => p.PublicationId, opt => opt.MapFrom(p => p.PublicationId))
         .ForMember(p => p.PublicationName, opt => opt.MapFrom(p => p.PublicationName))
         .ForMember(p => p.Pages, opt => opt.MapFrom(p => p.Pages))
         .ForMember(p => p.Publisher, opt => opt.MapFrom(p => p.Publisher))
         .ForMember(p => p.PublicationDate, opt => opt.MapFrom(p => p.PublicationDate))
         .ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription))
         .ForMember(p => p.Authors, opt => opt.MapFrom(p => p.Authors.ToList()));

            cfg.CreateMap<PublicationViewModel, Publication>()
            .ForMember(p => p.PublicationId, opt => opt.MapFrom(p => p.PublicationId))
            .ForMember(p => p.PublicationName, opt => opt.MapFrom(p => p.PublicationName))
            .ForMember(p => p.Pages, opt => opt.MapFrom(p => p.Pages))
            .ForMember(p => p.Publisher, opt => opt.MapFrom(p => p.Publisher))
            .ForMember(p => p.PublicationDate, opt => opt.MapFrom(p => p.PublicationDate))
            .ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription))
            .ForMember(p => p.Authors, opt => opt.MapFrom(p => p.Authors));

        });
    }
}

My models:

 public class PublicationViewModel
{
    public PublicationViewModel()
    {
        Authors = new List<AuthorViewModel>();
    }
    public int PublicationId { get; set; }

    [Display(Name = "Publication Name")]
    public string PublicationName { get; set; }

    public int Pages { get; set; }

    public string Publisher { get; set; }

    [Display(Name = "Publication Date")]
    public DateTime PublicationDate { get; set; }

    public SubscriptionViewModel Subscription { get; set; }

    [UIHint("AuthorsEditor")]
    public virtual List<AuthorViewModel> Authors { get; set; }
}
 public class Publication
{
    public Publication()
    {

    }
    public Publication(Publication publication)
    {
        PublicationId = publication.PublicationId;
        PublicationName = publication.PublicationName;
        Pages = publication.Pages;
        Publisher = publication.Publisher;
        Subscription = publication.Subscription;
        PublicationDate = publication.PublicationDate;
        Authors = publication.Authors;
    }

    public int PublicationId { get; set; }

    public string PublicationName { get; set; }

    public int Pages { get; set; }

    public string Publisher { get; set; }

    public DateTime PublicationDate { get; set; }

    public Subscription Subscription { get; set; }

    public virtual ICollection<Author> Authors { get; set; }
}

Upvotes: 2

Views: 3951

Answers (1)

Vlad
Vlad

Reputation: 233

I've found a problem. I forgot to send id from my subscriptionViewModel(Subscription is an enum type).

.ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription.SubscriptionId))//here

Upvotes: 1

Related Questions