在Automapper中映射时添加对象

时间:2017-07-20 22:16:17

标签: c# automapper

我正在尝试从Entity Framework对象映射到DTO对象,我在automapper配置文件中收到错误:

The type 'ClaimToTemplateClaim' cannot be used as typed parameter
'TValueResolver' in the generic type or method
'IMemberConfigurationExpression<TemplateEntity, Template,object>.ResolveUsing<TValueResolver>()'.
There is no implicit reference conversion from 'ClaimToTemplateClaim'
to 'AutoMapper.IValueResolver<GPS.Auth2.Service.DataAccess.Objects.TemplateEntity, GPSAuth2ServiceInterface.Objects.Template, object>.'

源对象DTO

public class Template
  {
    public Template()
    {
      this.Claims = new List<Claim>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<Claim> Claims { get; set; }
  }


public class Claim
  {
    public int Id { get; set; }
    public int ProductNumber { get; set; }
    public string TheClaim { get; set; }
  }

实体对象(添加联接表)

public class TemplateEntity
  {
    public TemplateEntity()
    {
      this.TemplateClaims = new List<TemplateClaimEntity>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<TemplateClaimEntity> TemplateClaims { get; set; }
  } 

public class TemplateClaimEntity
  {
    public int TemplateId { get; set; }
    public int ClaimId { get; set; }
    public ClaimEntity Claim { get; set; }
    public TemplateEntity Template { get; set; }
  }

public class TemplateEntity
  {
    public TemplateEntity()
    {
      this.TemplateClaims = new List<TemplateClaimEntity>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
  }

当有人通过我的声明时,我需要它实际上映射到ClaimEntity对象,但也创建一个TemplateClaimObject。

AutoMapper个人资料

public Auth2SetupMappings()
{    
  this.CreateMap<Template, TemplateEntity>()
    .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
    .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
    .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
    .ForMember(dest => dest.TemplateClaims, opt => opt.ResolveUsing<TemplateClaimToClaim>()) <- ERROR HERE
    .ReverseMap()
    .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
    .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
    .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
    .ForMember(dest => dest.Claims, opt => opt.ResolveUsing<ClaimToTemplateClaim>()); <- ERROR HERE
}


public class ClaimToTemplateClaim : IValueResolver<Claim, TemplateClaimEntity, TemplateClaimEntity>
{
  public TemplateClaimEntity Resolve(Claim source, TemplateClaimEntity destination, TemplateClaimEntity member, ResolutionContext context)
  {
    return new TemplateClaimEntity
    {
      Claim = new ClaimEntity
      {
        Claim = source.TheClaim,
        Id = source.Id,
        ProductNumber = source.ProductNumber
       }
    };
  }
}

public class TemplateClaimToClaim : IValueResolver<TemplateClaim, Claim, Claim>
  {
    public Claim Resolve(TemplateClaim source, Claim destination, Claim member, ResolutionContext context)
    {
      return new Claim
      {
        ProductNumber = source.Claim.ProductNumber,
        Id = source.Claim.Id,
        TheClaim = source.Claim.TheClaim
      };
    }
  }

基本上我们不想将连接表传递给DTO。

0 个答案:

没有答案