在 ASP.NET MVC 应用程序中初始化 AutoMapper v6 时出错
Error on initializing AutoMapper v6 in ASP.NET MVC App
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<SomeSourceModel, SomeDestinationModel>();
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
我在项目中重复这些代码。想创建一个通用接口 IMapper 以便我可以在需要使用时调用它。
我创建的解决方案是
private IMapper Mapper(TSource source, TDestination dest)
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<source, dest>();
});
config.AssertConfigurationIsValid();
return config.CreateMapper();
}
没用。问题是我不能以这种方式将源模型和目标模型作为参数传递。如何解决?
更新 1:
如@12seconds所述,我在Global.asax.cs
中开始初始化MapperConfigration
在 App_Start 文件夹中,我创建了
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<SourceModel1, DestinationModel1>();
CreateMap<SourceModel2, DestinationModel2>();
CreateMap<SourceModel3, DestinationModel3>();
CreateMap<SourceModel4, DestinationModel4>();
CreateMap<SourceModel5, DestinationModel5>();
Mapper.AssertConfigurationIsValid();
}
}
在Global.asax.cs
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<MappingProfile>();
});
}
}
然后我尝试在几个地方调用 AutoMapperConfiguration.Configure();
。当我启动 运行 应用程序时,我得到了相同的 错误消息 :
Mapper not initialized. Call Initialize with appropriate
configuration. If you are trying to use mapper instances through a
container or otherwise, make sure you do not have any calls to the
static Mapper.Map methods, and if you're using ProjectTo or
UseAsDataSource extension methods, make sure you pass in the
appropriate IConfigurationProvider instance.
我应该在哪里打电话给 AutoMapperConfiguration.Configure();
?我错过了什么吗?
问题已解决。 Mapper.AssertConfigurationIsValid();
应在 Mapper 初始化后执行。
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<MappingProfile>();
});
Mapper.Configuration.AssertConfigurationIsValid();
}
}
版本 5.0.x +
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<MappingProfile>();
});
Mapper.AssertConfigurationIsValid();
}
}
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<SomeSourceModel, SomeDestinationModel>();
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
我在项目中重复这些代码。想创建一个通用接口 IMapper 以便我可以在需要使用时调用它。
我创建的解决方案是
private IMapper Mapper(TSource source, TDestination dest)
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<source, dest>();
});
config.AssertConfigurationIsValid();
return config.CreateMapper();
}
没用。问题是我不能以这种方式将源模型和目标模型作为参数传递。如何解决?
更新 1:
如@12seconds所述,我在Global.asax.cs
MapperConfigration
在 App_Start 文件夹中,我创建了
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<SourceModel1, DestinationModel1>();
CreateMap<SourceModel2, DestinationModel2>();
CreateMap<SourceModel3, DestinationModel3>();
CreateMap<SourceModel4, DestinationModel4>();
CreateMap<SourceModel5, DestinationModel5>();
Mapper.AssertConfigurationIsValid();
}
}
在Global.asax.cs
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<MappingProfile>();
});
}
}
然后我尝试在几个地方调用 AutoMapperConfiguration.Configure();
。当我启动 运行 应用程序时,我得到了相同的 错误消息 :
Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.
我应该在哪里打电话给 AutoMapperConfiguration.Configure();
?我错过了什么吗?
问题已解决。 Mapper.AssertConfigurationIsValid();
应在 Mapper 初始化后执行。
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<MappingProfile>();
});
Mapper.Configuration.AssertConfigurationIsValid();
}
}
版本 5.0.x +
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<MappingProfile>();
});
Mapper.AssertConfigurationIsValid();
}
}