簡體   English   中英

使用 ProjectTo<> 時從“對象”到“對象”缺少 Map

[英]Missing Map from 'Object' to 'Object' when using ProjectTo<>

我正在研究 Web API 並使用 AutoMapper(10.1.1) 將映射從域模型轉換為應用程序模型

我已經在應用層注冊了 AutoMapper

 public static IServiceCollection ConfigureAppServices(this IServiceCollection services)
    {
        services.AddAutoMapper(Assembly.GetExecutingAssembly());
        services.AddMediatR(Assembly.GetExecutingAssembly());
        
        //services.AddMvc().AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<StartUp>())
        // services.AddControllersWithViews();
        
        return services;
    }

在 Web API startup.cs 中使用了這個 class

services.ConfigureAppServices();

我有一個名為 IMapFrom 的映射通用接口

public interface IMapFrom<T>
{
  void Mapping(Profile profile) => profile.CreateMap(typeof(T), GetType());
}

映射是從“JobModel”到“JobDetailsDto”

public class JobDetailsDto: IMapFrom<JobModel>
{
    public JobDetailsDto()
    {

    }
    public string Id { get; set; }
    public string Name { get; set; }
    public string JobType { get; set; }
    public string ShipBranch { get; set; }
    public string ProjectManager { get; set; }

    public void Mapping(Profile profile)
    {
        profile.CreateMap<JobModel, JobDetailsDto>()
            .ForMember(d => d.ShipBranch, opt => opt.MapFrom(s => s.PriceBranch))
            .ForMember(d => d.ProjectManager, opt => opt.MapFrom(s => s.BidderId));
    }
}

作業模型.cs

public class JobModel
{
    public JobModel()
    {
    }

    public int Id { get; set; }
    public DateTime BidDate { get; set; }
    public string JobType { get; set; }
    public DateTime CreatedDate { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string BidderId { get; set; }
    public string PriceBranch { get; set; }
    public string Name { get; set; }

}

我正在使用 MediaTr 處理 API 請求

public async Task<JobDetailsDto> Handle(GetJobDetailsQuery request, CancellationToken cancellationToken)
    {
        var vm = await _context.JobModels
                .Where(e => e.Id == request.Id)
                .ProjectTo<JobDetailsDto>(_mapper.ConfigurationProvider)
                .SingleOrDefaultAsync(cancellationToken);

        return vm;
    }

當我執行 'GET' 獲取 'JobModel' object 中的數據並轉換為 'JobDetailsDto' 時拋出以下錯誤

System.InvalidOperationException:缺少從 Domain.Entities.JobModel 到 Application.ApplicationBusiness.Job.Queries.JobDetailsDto 的 map。 使用 CreateMap<JobModel, JobDetailsDto> 創建。 在 AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest 請求,IDictionary 2 typePairCount,LetPropertyMaps letPropertyMaps)在 AutoMapper.QueryableExtensions.ExpressionBuilder 的 AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpressionCore(ExpressionRequest 請求,表達式實例參數,IDictionary 2 typePairCount, LetPropertyMaps letPropertyMaps, TypeMap& typeMap) at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, IDictionary .CreateMapExpression(ExpressionRequest request) at AutoMapper.Internal.LockingConcurrentDictionary 2.<>c__DisplayClass2_1.<.ctor>b__1() at System.Lazy 1.ViaFactory(LazyThreadSafetyMode 模式) at System.Lazy 1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy System.Lazy 1.CreateValue() 在 System.Lazy 1.get_Value() at AutoMapper.Internal.LockingConcurrentDictionary 2.GetOrAdd(TKey key) 在 AutoMapper.QueryableExtensions.ExpressionBuilder.GetMapExpression(Type sourceType, Type destinationType, Z49703179 4414A552435F90151AC3B54BZ parameters, MemberInfo[] membersToExpand) at AutoMapper.QueryableExtensions.ProjectionExpression.ToCore(Type destinationType, Object parameters, IEnumerable 1 memberPathsToExpand) at AutoMapper.QueryableExtensions.ProjectionExpression.ToCore[TResult](Object parameters, IEnumerable 1 memberPathsToExpand) at AutoMapper.QueryableExtensions .ProjectionExpression.To[TResult](對象參數,表達式1[] membersToExpand) at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Object parameters, Expression 1[] membersToExpand)在 AutoMapper.QueryableExtensions.Extensions .ProjectTo[Tdestination](IQueryable 源,IConfigurationProvider 配置,Expression`1[] membersToExpand)

嘗試這個。

public JobDetailsDto()
 {
    Mapping();
 }
 public void Mapping()
 {
    CreateMap<JobModel, JobDetailsDto>()
        .ForMember(d => d.ShipBranch, opt => opt.MapFrom(s => s.PriceBranch))
        .ForMember(d => d.ProjectManager, opt => opt.MapFrom(s => s.BidderId));
 }

另一個解決方案不是在 dto 中編寫映射,而是應該創建 MappingProfile,您將在其中添加映射。 然后在您的服務中,您應該將其添加到構造函數中

_mapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile<MappingProfile>(); });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM