在.net中映射自动映射器中的问题

时间:2017-10-23 13:44:51

标签: c# .net automapper

我正在尝试使用AutoMapper映射我使用其他模型制作的视图模型。

这是映射代码:

Mapper.Initialize(config =>
        {

            #region Admin

            //Labels
            config.CreateMap<AddLabelViewModel, Label>();
            config.CreateMap<Label, AddLabelViewModel>();
            config.CreateMap<EditLabelViewModel, Label>();
            config.CreateMap<Label, EditLabelViewModel>();

            //Legislatures
            config.CreateMap<AddLegislatureViewModel, Legislature>();
            config.CreateMap<Legislature, AddLegislatureViewModel>();
            config.CreateMap<EditLegislatureViewModel, Legislature>();
            config.CreateMap<Legislature, EditLegislatureViewModel>();

            //Proposers
            config.CreateMap<AddProposerViewModel, Proposer>();
            config.CreateMap<Proposer, AddProposerViewModel>();
            config.CreateMap<EditProposerViewModel, Proposer>();
            config.CreateMap<Proposer, EditProposerViewModel>();

            //Committees
            config.CreateMap<AddCommitteeViewModel, Committee>();
            config.CreateMap<Committee, AddCommitteeViewModel>();
            config.CreateMap<EditCommitteeViewModel, Committee>();
            config.CreateMap<Committee, EditCommitteeViewModel>();

            //DraftLaws
            config.CreateMap<AddDraftLawViewModel, DraftLaw>();
            config.CreateMap<DraftLaw, AddDraftLawViewModel>();
            config.CreateMap<EditDraftLawViewModel, DraftLaw>();
            config.CreateMap<DraftLaw, EditDraftLawViewModel>();

            //DraftLawPhase
            config.CreateMap<AddDraftLawPhaseViewModel, DraftLawPhase>();
            config.CreateMap<DraftLawPhase, AddDraftLawPhaseViewModel>();
            config.CreateMap<EditDraftLawPhaseViewModel, DraftLawPhase>();
            config.CreateMap<DraftLawPhase, EditDraftLawPhaseViewModel>();

            //DocumentGroup
            config.CreateMap<AddDocumentGroupViewModel, DocumentGroup>();
            config.CreateMap<DocumentGroup, AddDocumentGroupViewModel>();
            config.CreateMap<EditDocumentGroupViewModel, DocumentGroup>();
            config.CreateMap<DocumentGroup, EditDocumentGroupViewModel>();

            //DocumentCommittee
            config.CreateMap<AddDocumentCommitteeViewModel, DocumentCommittee>();
            config.CreateMap<DocumentCommittee, AddDocumentCommitteeViewModel>();
            config.CreateMap<EditDocumentCommitteeViewModel, DocumentCommittee>();
            config.CreateMap<DocumentCommittee, EditDocumentCommitteeViewModel>();

            //Users
            config.CreateMap<AddUserViewModel, User>();
            config.CreateMap<User, AddUserViewModel>();
            config.CreateMap<EditUserViewModel, User>();
            config.CreateMap<User, EditUserViewModel>();

            #endregion

            #region Deputies

            // Deputy to DeputyViewItem
            config.CreateMap<Deputy, DeputyViewItem>()              
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.ID))
            .ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
            .ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName))
            .ForMember(dest => dest.Photo, opt => opt.MapFrom(src => src.Photo))
            .ForMember(dest => dest.SocialFacebook, opt => opt.MapFrom(src => src.SocialFacebook))
            .ForMember(dest => dest.SocialTwitter, opt => opt.MapFrom(src => src.SocialTwitter))
            .ForMember(dest => dest.SocialLinkedin, opt => opt.MapFrom(src => src.SocialLinkedin))
            .ForMember(dest => dest.PoliticalGroup, opt => opt.MapFrom(src => src.PoliticalGroup.Acronym))
            //.ForMember(dest => dest.Party, opt => opt.MapFrom(src => "PTK"))
            ;

            config.CreateMap<Deputy, ProfileViewModel>()
            .ForMember(dest => dest.Biography, opt => opt.MapFrom(src => src.Biography))
            .ForMember(dest => dest.Party, opt => opt.MapFrom(src => src.Party.Acronym))
            .ForMember(dest => dest.PoliticalGroup, opt => opt.MapFrom(src => src.PoliticalGroup.Acronym))
            .ForMember(dest => dest.Photo, opt => opt.MapFrom(src => src.Photo))
            ;

            config.CreateMap<CommitteeMember, CommitteeViewItem>()
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Committee.Name.Text))
            ;

            config.CreateMap<WealthDeclaration, WealthViewItem>()
            .ForMember(dest => dest.Year, opt => opt.MapFrom(src => src.Year))
            .ForMember(dest => dest.Link, opt => opt.MapFrom(src => src.Link))
            ;

            config.CreateMap<Deputy, ConnectViewModel>()
            .ForMember(dest => dest.SocialFacebook, opt => opt.MapFrom(src => src.SocialFacebook))
            .ForMember(dest => dest.SocialTwitter, opt => opt.MapFrom(src => src.SocialTwitter))
            .ForMember(dest => dest.SocialLinkedin, opt => opt.MapFrom(src => src.SocialLinkedin))
            .ForMember(dest => dest.DeputyId, opt => opt.MapFrom(src => src.ID))
            ;

            config.CreateMap<Answer, QuestionAnswerViewItem>()
            .ForMember(dest => dest.QuestionText, opt => opt.MapFrom(src => src.Question.Text.Text))
            .ForMember(dest => dest.QuestionName, opt => opt.MapFrom(src => src.Question.FullName))
            .ForMember(dest => dest.QuestionEmail, opt => opt.MapFrom(src => src.Question.Email))
            .ForMember(dest => dest.QuestionTel, opt => opt.MapFrom(src => src.Question.Tel))
            .ForMember(dest => dest.QuestionMunicipality, opt => opt.MapFrom(src => src.Question.Municipality.Label.Text))
            .ForMember(dest => dest.QuestionCreatedAt, opt => opt.MapFrom(src => src.Question.CreatedAt))
            .ForMember(dest => dest.DeputyId, opt => opt.MapFrom(src => src.Question.Deputy.ID))

            .ForMember(dest => dest.AnswerId, opt => opt.MapFrom(src => src.ID))
            .ForMember(dest => dest.AnswerText, opt => opt.MapFrom(src => src.Text.Text))
            .ForMember(dest => dest.AnswerCreatedAt, opt => opt.MapFrom(src => src.CreatedAt))
            ;

            config.CreateMap<ActivityPhoto, ActivityViewItem>()
            .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Activity.Title.Text))
            .ForMember(dest => dest.Date, opt => opt.MapFrom(src => src.Activity.Date))
            .ForMember(dest => dest.Photo, opt => opt.MapFrom(src => src.Photo))
            ;

            config.CreateMap<Speech, SpeechViewItem>()
           .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title.Text))
           .ForMember(dest => dest.Date, opt => opt.MapFrom(src => src.Date))
           .ForMember(dest => dest.FilePath, opt => opt.MapFrom(src => src.FileDocument.FilePath))
           .ForMember(dest => dest.DeputyId, opt => opt.MapFrom(src => src.Deputy.ID))
           ;

            config.CreateMap<Initiative, InitiativesViewItem>()
            .ForMember(dest => dest.DeputyId, opt => opt.MapFrom(src => src.Deputy.ID))
            .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title.Text))
            .ForMember(dest => dest.Date, opt => opt.MapFrom(src => src.Date))
            .ForMember(dest => dest.FilePath, opt => opt.MapFrom(src => src.File.FilePath))
            ;

            config.CreateMap<DeputyVote, VotingActivityViewItem>()
            .ForMember(dest => dest.DeputyId, opt => opt.MapFrom(src => src.Deputy.ID))
            .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.VotingActivity.Title.Text))
            .ForMember(dest => dest.Date, opt => opt.MapFrom(src => src.Date))
            .ForMember(dest => dest.FilePath, opt => opt.MapFrom(src => src.VotingActivity.File.FilePath))
            .ForMember(dest => dest.Vote, opt => opt.MapFrom(src => src.Vote))
            ;

            config.CreateMap<Municipality, MunicipalityViewItem>()
            .ForMember(dest => dest.MunicipalityId, opt => opt.MapFrom(src => src.ID))
            .ForMember(dest => dest.Municipality, opt => opt.MapFrom(src => src.Label.Text))
            ;

            config.CreateMap<Question, AskQuestionViewModel>()
            .ForMember(dest => dest.Question, opt => opt.MapFrom(src => src.Text.Text))
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.FullName))
            .ForMember(dest => dest.Eamil, opt => opt.MapFrom(src => src.Email))
            .ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.Tel))
            .ForMember(dest => dest.MunicipalityId, opt => opt.MapFrom(src => src.Municipality.ID))
            ;

            config.CreateMap<AskQuestionViewModel, Question>()
           .ForMember(dest => dest.Deputy.ID, opt => opt.MapFrom(src => src.DeputyId))
           .ForMember(dest => dest.Text.Text, opt => opt.MapFrom(src => src.Question))
           .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.Name))
           .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Eamil))
           .ForMember(dest => dest.Tel, opt => opt.MapFrom(src => src.PhoneNumber))
           .ForMember(dest => dest.Municipality.ID, opt => opt.MapFrom(src => src.MunicipalityId))
           ;

            #endregion

        });

以下是错误消息:

System.ArgumentException:Expression&#39; dest =&gt; dest.Deputy.ID&#39;必须解析为顶级成员而不是任何子对象的属性。请在子类型或AfterMap选项上使用自定义解析程序。 参数名称:lambdaExpression

1 个答案:

答案 0 :(得分:1)

您需要在AskQuestionModelDeputy

之间创建映射
config.CreateMap<AskQuestionModel, Deputy>()
.ForMember(x => x.Id, opt => opt.MapFrom(src=> src.DeputyId));

<强>更新 抱歉,我忘了添加Deputy模型本身的映射: .ForMember(dest => dest.Deputy, opt => opt.MapFrom(src => src))

此外,您对MunicipalityText实体也存在同样的问题。

所以你的完整映射将是:

config.CreateMap<AskQuestionViewModel, Question>()
       .ForMember(dest => dest.Deputy, opt => opt.MapFrom(src => src))
       .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src))
       .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.Name))
       .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Eamil))
       .ForMember(dest => dest.Tel, opt => opt.MapFrom(src => src.PhoneNumber))
       .ForMember(dest => dest.Municipality, opt => opt.MapFrom(src => src))
       ;

config.CreateMap<AskQuestionViewModel, Deputy>()
       .ForMember(dest => dest.ID, opt => opt.MapFrom(src => src.DeputyId));
config.CreateMap<AskQuestionViewModel, Municipality>()
       .ForMember(dest => dest.ID, opt => opt.MapFrom(src => src.MunicipalityId));
config.CreateMap<AskQuestionViewModel, Text>()
       .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Question));

现在应该可以了。