我想在我的业务层程序集加载时,将我的业务层中的所有映射注册到数据层,并将数据层到业务层类注册。当前,我正在使用静态类为我完成此任务:
public static class AutoMapperBootstrapper
{
public static void InitMappings()
{
Mapper.Initialize(a => a.AddProfile<MyProfile>());
}
}
但是每次我使用Mapper.Map和配置文件中添加的映射进行呼叫时,仍然会说缺少类型映射信息。
我应该如何解决这个问题?
最佳答案
应用程序启动时,您的Mapper.AddProfile
似乎没有调用。
尝试这个,
在Global.asax.cs
[Application_Start
]中,
protected void Application_Start(object sender, EventArgs e)
{
Mapper.AddProfile<MyProfile>();
}
而
MyProfile
如下所示,public class MyProfile : Profile
{
public override string ProfileName
{
get { return "Name"; }
}
protected override void Configure()
{
//// BL to DL
Mapper.CreateMap<BLCLASS, DLCLASS>();
//// and DL to BL
Mapper.CreateMap<DLCLASS, BLCLASS>();
}
}
关于c# - AutoMapper在装配件加载时添加配置文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13247907/