2013-02-18 69 views
0

請參閱this post的解決方案。隨機「丟失類型映射配置或不支持的映射。」 Automapper出錯

好吧,我終於明白了。 : 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(); 

而且在引導程序,我有:

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

雖然此錯誤時隨機很難調試,看看會發生什麼。我搜查了很多沒有適當的解決方案。

該錯誤是這樣:

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

映射類型:國家 - > CountryDTO MyApp.Domain.BoundedContext.Country - > MyApp.Application.BoundedContext.CountryDTO

目標路徑:List`1 [0]

Source值:MyApp的.Domain.BoundedContext.Country

我的項目是一個MVC 3項目,Automapper 2.2和統一的IoC ..

我將欣賞任何想法,建議或解決方案,並感謝您的答案。

+0

** [Check here](http://stackoverflow.com/a/16153063/2007801)** – 2013-04-30 16:23:21

回答

5

如果使用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映射,因爲你沒有指定它們。

+0

實際上,我已經用Mapper.Initialize(m => m.AddProfile () ); Mapper.AssertConfigurationIsValid();並且一切似乎都沒問題。另外我已經繪製了city-cityDto,但是這裏沒有編寫代碼片斷,因爲它似乎與錯誤無關,雖然我的Id是Guid,但我不會在我的地圖中忽略它。 – 2013-02-19 09:59:34

+0

那麼你的實體 - 國家,有一個ID?那麼請寫出準確的代碼,給你帶來麻煩。 – Nenad 2013-02-20 02:23:58

+0

對不起,所有實體都從定義Id的抽象EntityBase派生。順便說一下,當我在瀏覽器中打開一個視圖並修改完成後,我更改了一些代碼並構建它時,我得到了這個錯誤,我做了一次F5刷新。 – 2013-02-20 11:52:32

0

對我來說,這個錯誤必須與我把我的CreateMap<>()電話放在哪裏。我已經把它放在我的DTO的靜態初始化器中。當我將CreateMap<>()呼叫移到不太可愛的地方時,一切正常。

相關問題