0

解決策については、この投稿を参照してください。

わかりました、私はついにそれを理解しました。コードの AppDomain.CurrentDomain.GetAssemblies() 部分がマッピング アセンブリを取得しないことがあるため、欠落している間にエラーが発生します。アプリにすべてのアセンブリを強制的に検索させることでこのコードを置き換えると、私の問題は解決しました。

私のエンティティ:

    /// <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 には次のものがあります。

        Bootstrapper.Initialise();

そしてBootstrapperには次のものがあります:

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

私のアダプターはどこにありますか:

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 は次のとおりです。

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

そのため、「型マップの構成が見つからないか、マッピングがサポートされていません」というメッセージがランダムに表示されます。エラーの指摘:

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

このエラーはランダムに発生しますが、デバッグして何が起こるかを確認するのは困難です。適切な解決策がなく、多くのことを検索しました。

エラーは次のようになります。

タイプ マップ構成が欠落しているか、サポートされていないマッピングです。

マッピング タイプ: Country -> CountryDTO MyApp.Domain.BoundedContext.Country -> MyApp.Application.BoundedContext.CountryDTO

宛先パス: リスト`1[0]

ソース値: MyApp.Domain.BoundedContext.Country

私のプロジェクトは、Automapper 2.2 と Unity IoC を使用した MVC 3 プロジェクトです。

アイデア、アドバイス、解決策をいただければ幸いです。回答に感謝します。

4

2 に答える 2

4

使用Mapper.AssertConfigurationIsValid();すると、もう少し詳細な情報が得られます。

マップされていないメンバーが見つかりました。以下のタイプとメンバーを確認してください。カスタムマッピング式を追加するか、無視するか、カスタムリゾルバーを追加するか、ソース/宛先タイプを変更します

いずれの場合も、宛先モデルのすべてのプロパティをマップしておく必要があります。CityDTOとIdがありませんでした。ここ:

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

指定しなかったため、City-CityDTOに追加のマッピングが必要になる場合があります。

于 2013-02-18T12:46:38.510 に答える
0

私にとって、このエラーは、CreateMap<>()電話をかけた場所に関係していました。DTOの静的初期化子に入れました。CreateMap<>()呼び出しをあまりかわいくない場所に移動すると、すべて正常に機能しました。

于 2015-03-05T16:41:43.750 に答える