一切都很好,直到我将DataTime属性添加到我的Model和viewModel。所有数据都是正确的,但无论如何都会发生错误。 我认为错误出现是因为我添加了PublicationDate。 据我所知,Automapper2.0应该可以正常使用日期,所以我不知道它为什么会发生
此处出现错误:
Publication viewModelPublication = new Publication(Mapper.Map<PublicationViewModel, Publication>(viewModel));
我的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));
});
}
}
我的模特:
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; }
}
答案 0 :(得分:0)
我发现了一个问题。我忘了从我的subscriptionViewModel发送id(订阅是枚举类型)。
.ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription.SubscriptionId))//here