我有两节课:
public class Parametro
{
public int Id { get; set; }
public string Nombre { get; set; }
public int TipoDatoId { get; set; }
public TipoDato TipoDato { get; set; }
}
public class ParametroCreateViewModel
{
public string Nombre { get; set; }
public int TipoDatoId { get; set; }
}
当我以这种方式设置它时 _mapper.Map (model); 我收到以下错误:
未映射的成员被发现。查看下面的类型和成员。添加自定义映射表达式、忽略、添加自定义解析器或修改源/目标类型对于没有匹配的构造函数,添加无参数 ctor、添加可选参数或映射所有构造函数参数 ParametroCreateViewModel-> Parametro(Destination member列表)ProyectoTest.ServiciosCliente.Application.Parametros.Commands.AddFParametro.ParametroCreateViewModel-> Calidda.ServiciosCliente.Domain.Parametros.Parametro(目标成员列表)未映射的属性:Id
但是当我这样尝试时,如果它有效: Mapper.Map < ParametroCreateViewModel,Parametro > (model);
我的autofac如下:
builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile));
builder.Register(c => new MapperConfiguration(cfg =>
{
foreach (var profile in c.Resolve<IEnumerable<Profile>>())
{
cfg.AddProfile(profile);
}
})).AsSelf().SingleInstance();
builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();
我的 automapperconfig 是:
public class AutoMapperConfig : Profile
{
public static void Initialize()
{
Mapper.Initialize((config) =>
{
config.CreateMap<ParametroCreateViewModel, Parametro>()
.ForMember(dest => dest.Id, opt => opt.Ignore()).ReverseMap();
}
}
}
并声明我的 global.asax:
AutofacConfig.Register();
AutoMapperConfig.Initialize();
GlobalConfiguration.Configure(WebApiConfig.Register);
慕容森
慕莱坞森
随时随地看视频慕课网APP
相关分类