首页 > 解决方案 > 错误 CS0121:编译时自动映射器出现问题

问题描述

所以映射器在我的项目中不起作用

services.AddAutoMapper();

错误:

Startup.cs(112,21):错误 CS0121:以下方法或属性之间的调用不明确:“ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Assembly[])”和“ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Type[])” [/Users/admin/Desktop/YP/youplay/server/YouPlay.Api/YouPlay.Api.csproj]

当我更改为:

services.AddAutoMapper(typeof(Startup));

它是编译的,但是,当我试图使 req 到

    public async Task<ActionResult<BetOpenForm>> CreateBetAsync([FromBody]CreateBetForm bet)
    {
        BetOpenForm response;
        try
        {

            BetOpen newBet = await _betService.CreateBetOpenAsync(bet);
           response = _mapper.Map<BetOpenForm>(newBet);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex.Message);
           // return new BadRequestObjectResult(new ApiError("Something gone wrong ;("));
             return new BadRequestObjectResult(new ApiError(ex.Message));
        }

        return Ok(response);
    }

我得到:

在此处输入图像描述

楷模:

using System;
using System.Collections.Generic;

namespace YouPlay.Api.Controllers.ViewModels.Bet
{
    public class BetOpenForm
    {
        public string Id { get; set; }
        public int StreamId { get; set; }
        public List<BetOptionForm> BetOptions { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public string AuthorId { get; set; }
    }
}


using System;

namespace YouPlay.Api.Controllers.ViewModels.Bet
{
    public class BetOptionForm
    {
        public string Id { get; set; }
        public string BetOpenId { get; set; }
        public string OptionText { get; set; }
        public double Odd { get; set; }
        public decimal Amount { get; set; }
        public int BetsCount { get; set; }
    }
}

标签: c#.netautomapper

解决方案


推荐阅读