尝试将AutoMapper添加到Asp.net Core 2? [英] Trying to add AutoMapper to Asp.net Core 2?

查看:65
本文介绍了尝试将AutoMapper添加到Asp.net Core 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间我从事过一个asp.net core 1.1项目,并在projetc AutoMapper中使用.

I worked on a asp.net core 1.1 project a while ago and use in projetc AutoMapper.

在asp.net core 1.1中,我在启动文件中添加services.AddAutoMapper():

in asp.net core 1.1, I add services.AddAutoMapper() in startup file :

asp.net 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.net Core 2项目,并且在sturtap文件中出现services.AddAutoMapper()错误.

And everything was fine. But I'm currently working on a Asp.net 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.net核心2中的services.AddAutoMapper具有一些参数.我应该向该参数发送什么?

What is the reason for this error? Also, services.AddAutoMapper in asp.net 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 > https://www.nuget.org/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/3.0.1

For ASP.NET Core you need at least Version 3.0.1 from https://www.nuget.org/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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆