尝试将AutoMapper添加到Asp.net Core 2?

编程入门 行业动态 更新时间:2024-10-14 18:13:14
本文介绍了尝试将AutoMapper添加到Asp Core 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

前段时间我从事过一个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?

本文发布于:2023-11-16 06:54:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1602002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Asp   AutoMapper   Core   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!