作者:天天写程序 | 来源:互联网 | 2022-11-20 23:21
1> Manoj Choudh..:
You will have to use automapper package as shown below:
Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
This will also in turn install the Automapper nuget package if you don’t have it already.
Then, inside your ConfigureServices method of your startup.cs, you will have to add a call to it as shown below.
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper();
}
Refer this blog for more details.
EDIT:
There is very nice description from this thread.
You need to add code like below in startup.cs.
You have missed to add IMapper in DI. Please refer add Singleton call from below code.
public void ConfigureServices(IServiceCollection services) {
// .... Ignore code before this
// Auto Mapper Configurations
var mappingCOnfig= new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddMvc();
}
感谢您的时间。我已经添加了那个包。检查Startup.cs
更新了答案,希望这可以帮助您解决问题。
自从我开始一个新项目以来,需要一分钟,并且需要包含AutoMapper。添加服务时出现了折旧警告。AddAutoMapper(); 到startup.cs。您编辑的答案非常有帮助。谢谢!