Automapper 9 配置
Automapper 9 Configuration
在以前的 AutoMapper 版本中,我曾经能够像这样配置 AutoMapper:
public static class AutoMapperFactory
{
public static IConfigurationProvider CreateMapperConfiguration()
{
var config = new MapperConfiguration(cfg =>
{
//Scan *.UI assembly for AutoMapper Profiles
var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));
cfg.AddProfiles(assembly);
cfg.IgnoreAllUnmapped();
});
return config;
}
}
现在,cfg.AddProfiles(assembly)
行给我错误:Argument 1: cannot convert from 'System.Reflection.Assembly' to 'System.Collections.Generic.IEnumerable<AutoMapper.Profile>
如何让 IEnumerable<AutoMapper.Profile>
作为 AddProfiles
的参数传递?
您可以使用 addMaps 而不是 addProfile,如下所示:
public static class AutoMapperFactory
{
public static IConfigurationProvider CreateMapperConfiguration()
{
var config = new MapperConfiguration(cfg =>
{
//Scan *.UI assembly for AutoMapper Profiles
var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));
cfg.AddMaps(assembly);
cfg.IgnoreAllUnmapped();
});
return config;
}
}
如文档中所述:
Configuration inside a profile only applies to maps inside the
profile. Configuration applied to the root configuration applies to
all maps created.
并且可以创建为具有特定类型映射的 类。
在以前的 AutoMapper 版本中,我曾经能够像这样配置 AutoMapper:
public static class AutoMapperFactory
{
public static IConfigurationProvider CreateMapperConfiguration()
{
var config = new MapperConfiguration(cfg =>
{
//Scan *.UI assembly for AutoMapper Profiles
var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));
cfg.AddProfiles(assembly);
cfg.IgnoreAllUnmapped();
});
return config;
}
}
现在,cfg.AddProfiles(assembly)
行给我错误:Argument 1: cannot convert from 'System.Reflection.Assembly' to 'System.Collections.Generic.IEnumerable<AutoMapper.Profile>
如何让 IEnumerable<AutoMapper.Profile>
作为 AddProfiles
的参数传递?
您可以使用 addMaps 而不是 addProfile,如下所示:
public static class AutoMapperFactory
{
public static IConfigurationProvider CreateMapperConfiguration()
{
var config = new MapperConfiguration(cfg =>
{
//Scan *.UI assembly for AutoMapper Profiles
var assembly = Assembly.GetAssembly(typeof(AutoMapperFactory));
cfg.AddMaps(assembly);
cfg.IgnoreAllUnmapped();
});
return config;
}
}
如文档中所述:
Configuration inside a profile only applies to maps inside the profile. Configuration applied to the root configuration applies to all maps created.
并且可以创建为具有特定类型映射的 类。