-1

我在使用此模型的 Automapper 时遇到问题

public class Contact 
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string MessageTitle { get; set; }
        public string MessageBody { get; set; }
        public string MessageTime { get; set; }
    }

和这个 ViewModel

 public class ContactView
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string MessageTitle { get; set; }
        public string MessageBody { get; set; }
        public string MessageTime { get; set; }
    }

这是我的转换方法:

//Convert to Model
    public static Contact ConvertToContactModel(this ContactView contactView)
    {
        return Mapper.Map<ContactView, Contact>(contactView);

    }


 //Convert to ViewModel
     public static ContactView ConvertToContactView(this Contact contact)
            {
                return Mapper.Map<Contact, ContactView>(contact);
            }

为什么转换为模型(ConvertToContactModel)方法不起作用??

4

1 回答 1

3

确保在映射某些对象之前创建映射。您应该在应用程序启动(Main方法或Global.asaxApplication_Start中)有此代码:

Mapper.CreateMap<ContactView, Contact>();
Mapper.CreateMap<Contact, ContactView>();
于 2013-01-26T22:32:03.273 回答