map configuration or unsupported mapping

跟風遠走 提交于 2019-12-12 02:30:00

问题


I have two types. One in the business layer:

namespace Business
{
    public class Car
    {

        private int _id;
        private string _make;
        private string _model;

        public int id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string make
        {
            get { return _make; }
            set { _make = value; }
        }

        public string model
        {
            get { return _model; }
            set { _model = value; }
        }

    }

}

and the other in the Data layer (Entity Framework):

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

    public partial class Car
    {
        public Car()
        {
            this.facttables = new HashSet<facttable>();
        }

        public int id { get; set; }
        public string make { get; set; }
        public string model { get; set; }

        public virtual ICollection<facttable> facttables { get; set; }
    }
}

Here is the code I get from the service layer:

    namespace Data
{
    public class VehicleDAO : IVehicleDAO
    {

        private static readonly ILog log = LogManager.GetLogger(typeof(VehicleDAO));

        MapperConfiguration config;

        public VehicleDAO ()
        {
            Mapper.Initialize(cfg => cfg.CreateMap<Business.Car, Data.Car>());
            config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Business.Car, Data.Car>()
                    .ForMember(dto => dto.facttables, opt => opt.Ignore());
                    //.ForMember(d => d.id, opt => opt.MapFrom(c => c.id))
                    //.ForMember(d => d.make, opt => opt.MapFrom(c => c.make))
                    //.ForMember(d => d.model, opt => opt.MapFrom(c => c.model));
            });
            config.AssertConfigurationIsValid();
        }

        public Data.Car Select(int id)
        {
            Data.Car car;
            using (VehicleEntities VehicleDatabase = new VehicleEntities())
            {
                car = VehicleDatabase.Cars.Where(c => c.id == id).ToList().Single();
                Business.Car cars = AutoMapper.Mapper.Map<Business.Car>(car);
            }
            return car;
        }

The exception is: {"Missing type map configuration or unsupported mapping.\r\n\r\nMapping types:\r\nCar_70BD8401A87DAAD8F5F0EC35BCAE5C9E6EE2D6CB5A1AFCE296B313D8AD87D2E9 -> Car\r\nSystem.Data.Entity.DynamicProxies.Car_70BD8401A87DAAD8F5F0EC35BCAE5C9E6EE2D6CB5A1AFCE296B313D8AD87D2E9 -> Business.Car"}. What is wrong? I have marked the line that causes the exception (third from last line).


回答1:


Automapper only maps in the direction you created the mapping in. CreateMap<Business.Car, Data.Car> creates a mapping from Business.Car to Data.Car. It looks like you are trying to map from Data.Car to Business.Car, which means you need to CreateMap<Data.Car, Business.Car>

Mapper.Initialize(cfg => cfg.CreateMap<Data.Car, Business.Car>());
config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<Data.Car, Business.Car>();
});
config.AssertConfigurationIsValid();


来源:https://stackoverflow.com/questions/38378459/map-configuration-or-unsupported-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!