-1

I have a trouble in Automapper with this Model

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; }
    }

and this 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; }
    }

and this is my converting Methods :

//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);
            }

why convert to Model (ConvertToContactModel) method doesn't work ??

Ehsan
  • 816
  • 9
  • 27
  • 3
    **doesn't work** is not that clear... (still not clear : what do you mean by doesn't work ? Error thrown, result is not what you want ?) – Raphaël Althaus Jan 26 '13 at 22:26
  • 2
    Have you created the map first? `Mapper.CreateMap()` and vice-versa? – D Stanley Jan 26 '13 at 22:27
  • Didn't Got ur mean , I've mapping methods in separate class with this calling : public void CreateContact(CreateContactRequest request) { ContactView contact = new ContactView(); Contact modelContact = contact.ConvertToContactModel(); _contactRepository.Add(modelContact); _unitOfWork.Commit(); } – Ehsan Jan 26 '13 at 22:30

1 Answers1

4

Make sure you create mappings before mapping some objects. You should have this code on application start (Main method, or Application_Start in Global.asax):

Mapper.CreateMap<ContactView, Contact>();
Mapper.CreateMap<Contact, ContactView>();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459