Trying to add AutoMapper to Asp.net Core 2?

后端 未结 10 1656
我在风中等你
我在风中等你 2020-12-23 19:20

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

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

相关标签:
10条回答
  • 2020-12-23 19:54

    If you are using AspNet Core 2.2.Try changing your code from:

    services.AddAutoMapper();
    

    To:

    services.AddAutoMapper(typeof(Startup));
    

    It worked for me!

    0 讨论(0)
  • 2020-12-23 19:55

    You likely updated your ASP.NET Core dependencies, but still using outdated AutoMapper.Extensions.Microsoft.DependencyInjection package.

    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

    Which references AutoMapper 6.1.1 or higher.

    AutoMapper (>= 6.1.1)

    Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.0.0)

    Microsoft.Extensions.DependencyModel (>= 2.0.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

    0 讨论(0)
  • 2020-12-23 20:07

    Dec 6th 2019 Based upon initial attempt in a pluralsight course Building an API with ASP.NET Core by Shawn Wildermuth. As I got the error "...ambiguous 'ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Assembly[])..."

    I started researching proper syntax to implement AddAutoMapper in Core 2.2. My NuGet reference is version 7.0.0 After the tutorial had me create the Profile class in my Data repository directory which additionally referenced my model nir weiner & dev-siberia's answers above led me to trying to reference the profile class in the Startup.ConfigureServices() by name:

    services.AddAutoMapper(typeof(CampProfile));
    

    the content of the profile class is just a (no pun intended) old school map of the data class and the model in its constructor

    this.CreateMap<Camp, CampModel>();  
    

    This addressed the poor lack of documentation for this current version.

    Respectfully,

    ChristianProgrammer

    0 讨论(0)
  • 2020-12-23 20:08

    try this, works with 2.1 and up, i have not used any previous version so can't tell.

    services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

    0 讨论(0)
提交回复
热议问题