自动映射导致错误:缺少类型映射配置或不支持的映射

时间:2014-07-09 05:05:33

标签: entity-framework-6 nuget-package automapper-3

我是Automapper的新手。

我已将Nuget包 - Automapper添加到我的管理器(BLL)和DAL层。

现在,下面是相关内容:

以下是管理器库的声明,它给了我例外:

this.dataRepository.Update(Mapper.Map<StudentMaster>(studentDTO));

例外情况如下:

  

缺少类型映射配置或不支持的映射。

     

映射类型:
  studentDTO - &gt; StudentMaster
  Admin.App.DTO.studentDTO-&GT; Admin.App.DAL.StudentMaster

如果选择/在EF上查询,它可以使用

进行映射
.Project().To<TReturn>()

我写了一个Autoconfiguration.cs文件如下:

public static class AutoMapperConfiguration
{
    public static void Configure()
    {
        ConfigureStudentMasterMaps();
    }

    private static void ConfigureStudentMasterMaps()
    {
        Mapper.CreateMap<StudentMaster, studentDTO>();                  
    }
}

注意:

实体 - StudentMaster(模型)实体和StudentDTO都具有相同的属性。

请指导我如何解决此问题。

谢谢

2 个答案:

答案 0 :(得分:8)

请参阅入门https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

CreateMap<**TSource, TDestination**>()

您必须添加

Mapper.CreateMap<studentDTO, StudentMaster>();

映射配置调用后

Mapper.AssertConfigurationIsValid();

答案 1 :(得分:1)

我有类似的问题,我忘记在Global.asax中注册

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);


        AutoMapperConfig.RegisterMappings();

    }
}
相关问题