前段时间我从事过一个asp core 1.1项目,并在projetc AutoMapper中使用.
I worked on a asp core 1.1 project a while ago and use in projetc AutoMapper.
在asp core 1.1中,我在启动文件中添加services.AddAutoMapper():
in asp core 1.1, I add services.AddAutoMapper() in startup file :
asp core 1.1中的启动文件:
public void ConfigureServices(IServiceCollection services) { //Some Code services.AddMvc(); services.AddAutoMapper(); }而且我可以轻松地在Controller中使用AutoMapper.
And I use AutoMapper in Controller easily.
控制器:
public async Task<IActionResult> AddEditBook(AddEditBookViewModel model) { Book bookmodel = AutoMapper.Mapper.Map<AddEditBookViewModel, Book>(model); context.books.Add(bookmodel); context.SaveChanges(); }一切都很好. 但是我目前正在研究Asp Core 2项目,并且在sturtap文件中出现services.AddAutoMapper()错误.
And everything was fine. But I'm currently working on a Asp Core 2 project and I get the error with services.AddAutoMapper() in sturtap file.
错误CS0121,以下方法或属性之间的调用不明确:'ServiceCollectionExtensions.AddAutoMapper(IServiceCollection,params Assembly [])'和'ServiceCollectionExtensions.AddAutoMapper(IServiceCollection,params Type [])'
Error CS0121 The call is ambiguous between the following methods or properties: 'ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Assembly[])' and 'ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Type[])'
此错误的原因是什么? 另外,asp核心2中的services.AddAutoMapper具有一些参数.我应该向该参数发送什么?
What is the reason for this error? Also, services.AddAutoMapper in asp core 2 has some parameters. what should I send to this parameter?
推荐答案您可能更新了ASP.NET Core依赖项,但仍使用过时的AutoMapper.Extensions.Microsoft.DependencyInjection包.
You likely updated your ASP.NET Core dependencies, but still using outdated AutoMapper.Extensions.Microsoft.DependencyInjection package.
对于ASP.NET Core,您至少需要3.0.1 > www.nuget/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/3.0.1
For ASP.NET Core you need at least Version 3.0.1 from www.nuget/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/3.0.1
哪个引用了AutoMapper 6.1.1或更高版本.
Which references AutoMapper 6.1.1 or higher.
AutoMapper(> = 6.1.1)
AutoMapper (>= 6.1.1)
Microsoft.Extensions.DependencyInjection.Abstractions(> = 2.0.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.0.0)
Microsoft.Extensions.DependencyModel(> = 2.0.0)
Microsoft.Extensions.DependencyModel (>= 2.0.0)
较早的软件包取决于Microsoft.Extensions.DependencyInjection.Abstractions 1.1.0,因此不能与ASP.NET Core一起使用,因为Microsoft.Extensions.DependencyInjection.Abstractions 1.1.0和2.0
The older packages depend on Microsoft.Extensions.DependencyInjection.Abstractions 1.1.0 and can't be used with ASP.NET Core since there have been breaking changes between Microsoft.Extensions.DependencyInjection.Abstractions 1.1.0 and 2.0
更多推荐
尝试将AutoMapper添加到Asp.net Core 2?
发布评论