w0051977
w0051977

Reputation: 15807

Cannot map Business Object to Data Object

I am really pulling my hair out with the Automapper. I find it unintuitive and very difficult to use. It would be so much easier just to write the mapping code manually. I have two simple types:

    namespace BusinessObjects
    {
        public class Application
        {
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string Surname { get; set; }
            public System.DateTime DateOfBirth { get; set; }
            public System.DateTime CreatedTime { get; set; }
            public System.DateTime ModifiedTime { get; set; }
            public System.DateTime ApplicationDate { get; set; }
            public string Qualified { get; set; }
            public int salary { get; set; }
            public string userID { get; set; }
    }
    }

namespace DataObjects
{
    using System;
    using System.Collections.Generic;

    public partial class Application
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public System.DateTime DateOfBirth { get; set; }
        public System.DateTime CreatedTime { get; set; }
        public System.DateTime ModifiedTime { get; set; }
        public System.DateTime ApplicationDate { get; set; }
        public string Qualified { get; set; }
        public int salary { get; set; }
        public string userID { get; set; }
    }
}

and the Controller:

public ApplicationController()
        {
            Mapper.Initialize(cfg => cfg.CreateMap<BusinessObjects.Application, ApplicationModel>());
            config = new MapperConfiguration(cfg =>

            {
                cfg.CreateMap<BusinessObjects.Application, ApplicationModel>();
            });
            config.AssertConfigurationIsValid();

            Mapper.Initialize(cfg => cfg.CreateMap<ApplicationModel,BusinessObjects.Application>());
            config2 = new MapperConfiguration(cfg =>

            {
                cfg.CreateMap<ApplicationModel,BusinessObjects.Application>();
            });
            config2.AssertConfigurationIsValid();

        }

    public int Create(BusinessObjects.Application businessApplication)
            {
                var mapper =config2.CreateMapper();
                DataObjects.Application dataApplication = AutoMapper.Mapper.Map<DataObjects.Application>(businessApplication);

                int count = 0;
                using (CreditCardPreQualificationEntities CreditCardPreQualificationDatabase = new CreditCardPreQualificationEntities())
                {
                    CreditCardPreQualificationDatabase.Applications.Add(dataApplication);
                    count = CreditCardPreQualificationDatabase.SaveChanges();
                }
                return count;
            }

I spent nearly all weekend trying to get this to work and am no closer. I cannot see the benefit of Automapper at all. The error I get is: "An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code

Additional information: Missing type map configuration or unsupported mapping."Please help.

Upvotes: 0

Views: 95

Answers (1)

Jimmy Bogard
Jimmy Bogard

Reputation: 26765

Your configuration looks....off. As the docs say, you need to initialize AutoMapper once and only once for the entire AppDomain. In your global.asax (or similar), add:

Mapper.Initialize(cfg => {
    cfg.CreateMap<BusinessObjects.Application, DataObjects.Application>()
       .ReverseMap();
});

It looks like you need maps in both directions, so I added ReverseMap. Then in your controller, remove all that initialization and mapper creation junk, and just use AutoMapper directly:

    public int Create(BusinessObjects.Application businessApplication)
        {
            DataObjects.Application dataApplication = Mapper.Map<BusinessObjects.Application, DataObjects.Application>(businessApplication);

            int count = 0;
            using (CreditCardPreQualificationEntities CreditCardPreQualificationDatabase = new CreditCardPreQualificationEntities())
            {
                CreditCardPreQualificationDatabase.Applications.Add(dataApplication);
                count = CreditCardPreQualificationDatabase.SaveChanges();
            }
            return count;
        }

Done!

Upvotes: 1

Related Questions