无法将业务对象映射到数据对象

时间:2016-07-18 17:56:46

标签: c# entity-framework automapper

我真的用Automapper把头发拉了出来。我发现它不直观且很难使用。只需手动编写映射代码就会容易得多。我有两种简单的类型:

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

和控制器:

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

我花了差不多整个周末试图让这个工作起来并且不再接近。我根本看不到Automapper的好处。我得到的错误是:&#34;类型&#39; AutoMapper.AutoMapperMappingException&#39;发生在AutoMapper.dll中但未在用户代码中处理

其他信息:缺少类型映射配置或不支持的映射。&#34;请帮助。

1 个答案:

答案 0 :(得分:1)

您的配置看起来......关闭。正如文档所说,您需要为整个AppDomain初始化AutoMapper一次且仅初始化一次。在你的global.asax(或类似的)中,添加:

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

看起来你需要两个方向的地图,所以我添加了ReverseMap。然后在您的控制器中,删除所有初始化和映射器创建垃圾,并直接使用AutoMapper:

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

完成!