地图配置或不支持的映射 [英] map configuration or unsupported mapping

查看:224
本文介绍了地图配置或不支持的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种类型。一个在业务层:

 命名空间业务
{
public class Car
{

private int _id;
私人字符串_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; }
}

}

}

另一个在数据层(实体框架)中:

 命名空间数据
{
使用系统;
使用System.Collections.Generic;

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

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

public virtual ICollection< facttable>事实{get;组; }
}
}

这是从服务层得到的代码:

 命名空间数据
{
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;
使用(VehicleEntities VehicleDatabase = new VehicleEntities())
{
car = VehicleDatabase.Cars.Where(c => c.id == id).ToList()。 ;
Business.Car cars = AutoMapper.Mapper.Map< Business.Car>(car);
}
回车;
}

异常是: {缺少类型映射配置或不支持的mapping.\r\\\
\r\\\
Mapping类型:\r\\\
Car_70BD8401A87DAAD8F5F0EC35BCAE5C9E6EE2D6CB5A1AFCE296B313D8AD87D2E9 - > Car\r\\\
System.Data.Entity.DynamicProxies.Car_70BD8401A87DAAD8F5F0EC35BCAE5C9E6EE2D6CB5A1AFCE296B313D8AD87D2E9 - > Business.Car}
。哪里不对?我已经标记了导致异常的行(最后一行的第三行)。

解决方案

Automapper只会沿您创建的方向映射映射in。CreateMap < Business.Car,Data.Car >创建从Business.Car到Data.Car的映射。看来您正在尝试从Data.Car映射到Business.Car,这意味着您需要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();


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).

解决方案

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();

这篇关于地图配置或不支持的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆