随机"缺少类型映射配置或不支持的映射和QUOT;错误Automapper [英] Random "Missing type map configuration or unsupported mapping." Error in Automapper

查看:382
本文介绍了随机"缺少类型映射配置或不支持的映射和QUOT;错误Automapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实体:

    /// <summary>
    /// Get/Set the name of the Country
    /// </summary>
    public string CountryName { get; set; }

    /// <summary>
    /// Get/Set the international code of the Country
    /// </summary>
    public string CountryCode { get; set; }

    /// <summary>
    /// Get/Set the coordinate of the Country
    /// </summary>
    public Coordinate CountryCoordinate { get; set; }

    /// <summary>
    /// Get/Set the cities of the country
    /// </summary>
    public virtual ICollection<City> Cities
    {
        get
        {
            if (_cities == null)
            {
                _cities = new HashSet<City>();
            }

            return _cities;
        }
        private set
        {
            _cities = new HashSet<City>(value);
        }
    }

我的DTO:

    public Guid Id { get; set; }

    public string CountryName { get; set; }

    public string CountryCode { get; set; }

    public string Lattitude { get; set; }

    public string Longtitude { get; set; }

    public List<CityDTO> Cities { get; set; }

我的配置

        // Country => CountryDTO
        var countryMappingExpression = Mapper.CreateMap<Country, CountryDTO>();
        countryMappingExpression.ForMember(dto => dto.Lattitude, mc => mc.MapFrom(e => e.CountryCoordinate.Lattitude));
        countryMappingExpression.ForMember(dto => dto.Longtitude, mc => mc.MapFrom(e => e.CountryCoordinate.Longtitude));

在Global.asax中的Application_Start我有:

In Global.asax Application_Start I have:

        Bootstrapper.Initialise();

和在引导程序我有:

public static class Bootstrapper
{
    private static IUnityContainer _container;

    public static IUnityContainer Current
    {
        get
        {
            return _container;
        }
    }

    public static void Initialise()
    {
        var container = BuildUnityContainer();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }

    private static IUnityContainer BuildUnityContainer()
    {
        _container = new UnityContainer();

        _container.RegisterType(typeof(BoundedContextUnitOfWork), new PerResolveLifetimeManager());

        _container.RegisterType<ICountryRepository, CountryRepository>();  

        _container.RegisterType<ITypeAdapterFactory, AutomapperTypeAdapterFactory>(new ContainerControlledLifetimeManager());

        _container.RegisterType<ICountryAppService, CountryAppServices>(); 

        EntityValidatorFactory.SetCurrent(new DataAnnotationsEntityValidatorFactory());
        var typeAdapterFactory = _container.Resolve<ITypeAdapterFactory>();
        TypeAdapterFactory.SetAdapter(typeAdapterFactory);

        return _container;
    }
}

我在哪里适配器是:

Where my adapter is:

public class AutomapperTypeAdapter : ITypeAdapter
{
    public TTarget Adapt<TSource, TTarget>(TSource source)
        where TSource : class
        where TTarget : class, new()
    {
        return Mapper.Map<TSource, TTarget>(source);
    }

    public TTarget Adapt<TTarget>(object source) where TTarget : class, new()
    {
        return Mapper.Map<TTarget>(source);
    }
}

和AdapterFactory是:

And AdapterFactory is:

    public AutomapperTypeAdapterFactory()
    {
        //Scan all assemblies to find an Auto Mapper Profile
        var profiles = AppDomain.CurrentDomain
                                .GetAssemblies()
                                .SelectMany(a => a.GetTypes())
                                .Where(t => t.BaseType == typeof(Profile));

        Mapper.Initialize(cfg =>
        {
            foreach (var item in profiles)
            {
                if (item.FullName != "AutoMapper.SelfProfiler`2")
                    cfg.AddProfile(Activator.CreateInstance(item) as Profile);
            }
        });
    }

所以我的随机得到一个缺少类型映射配置或不支持的映射。错误指向:

So I randomly get a "Missing type map configuration or unsupported mapping." error pointing:

    public TTarget Adapt<TTarget>(object source) where TTarget : class, new()
    {
        return Mapper.Map<TTarget>(source);
    }

虽然出现此错误的随机很难调试,看看会发生什么。我寻觅了很多,没有妥善的解决办法。

While this error occurs randomly it is hard to debug and see what happens. I have searched a lot with no proper solution.

错误是这样:

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

Missing type map configuration or unsupported mapping.

映射类型:国家 - > CountryDTO
  MyApp.Domain.BoundedContext.Country - >
  MyApp.Application.BoundedContext.CountryDTO

Mapping types: Country -> CountryDTO MyApp.Domain.BoundedContext.Country -> MyApp.Application.BoundedContext.CountryDTO

目标路径:List`1 [0]

Destination path: List`1[0]

来源值:MyApp.Domain.BoundedContext.Country

Source value: MyApp.Domain.BoundedContext.Country

我的项目是一个MVC 3项目,Automapper 2.2和Unity的IoC ..

My project is an MVC 3 project with Automapper 2.2 and Unity IoC..

我将AP preciate为您解答任何想法,建议或解决方案和感谢。

I will appreciate any idea, advice or solution and thanks for your answers.

推荐答案

如果您使用 Mapper.AssertConfigurationIsValid(); 你会得到多一点的详细信息:

If you use Mapper.AssertConfigurationIsValid(); you would get bit more detailed info:

未映射成员被发现了。查看下面的类型和成员。添加
  自定义映射前pression,忽略,添加自定义解析,或修改
  源/目标类型

Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

在任何情况下,你必须已映射目标模型的所有属性。你失踪CityDT​​O和ID。这里:

In any case, you have to have mapped all properties of destination model. You were missing CityDTO and Id. Here:

Mapper.CreateMap<City, CityDTO>();

Mapper.CreateMap<Country, CountryDTO>()
    .ForMember(dto => dto.Id, options => options.Ignore())
    .ForMember(dto => dto.Longtitude, mc => mc.MapFrom(e => e.CountryCoordinate.Longtitude))
    .ForMember(dto => dto.Lattitude, mc => mc.MapFrom(e => e.CountryCoordinate.Lattitude));

也许你需要在市CityDT​​O一些额外的映射,如您没有指定他们。

Maybe you would need some additional mapping on City-CityDTO, as you did not specify them.

这篇关于随机&QUOT;缺少类型映射配置或不支持的映射和QUOT;错误Automapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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