請參閱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 ..
我將欣賞任何想法,建議或解決方案,並感謝您的答案。
** [Check here](http://stackoverflow.com/a/16153063/2007801)** – 2013-04-30 16:23:21