简体   繁体   中英

Automapper missing type map configuration

I can't seem to figure out what is going wrong here, I have configured AutoMapper as follows

services.AddAutoMapper(typeof(MetingenView), typeof(Meting));

And in the controller like this:

public MetingenController(IMapper mapper)
{
    this._mapper = mapper;
}

After, I use it like this:

var entity = await this.Context.MetingenView.AsNoTracking().FirstOrDefaultAsync(g =>g.IdMeting == key);
if (entity == null)
{
    return NotFound();
}
data.Patch(entity);
var meting = await this.Context.Meting.FirstOrDefaultAsync(m => m.IdMeting == key);
this._mapper.Map(entity, meting);

Then the error rolls out:

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

EDIT:

Here are the Meting, and MetingenView classes:

public partial class Meting
{
    public int IdMeting { get; set; }
    public int IdKoeling { get; set; }
    public int IdWerknemer { get; set; }
    public int IdGebouw { get; set; }
    public int Temperatuur { get; set; }
    public DateTime AfgenomenTijd { get; set; }
    public string ProductNaam { get; set; }
    public string Actie { get; set; }
    public DateTime? DatumOntstaan { get; set; }
    public DateTime? DatumMutatie { get; set; }
    public int IndVerwijderd { get; set; }
    public DateTime? DatumVerwijderd { get; set; }

    public virtual Gebouw IdGebouwNavigation { get; set; }
    public virtual Koeling IdKoelingNavigation { get; set; }
    public virtual Werknemer IdWerknemerNavigation { get; set; }
}



public partial class MetingenView
{
    [Key]
    public int IdKlant { get; set; }
    public string Locatie { get; set; }
    public string SoortKoeling { get; set; }
    public int IdMeting { get; set; }
    public int IdKoeling { get; set; }
    public int IdWerknemer { get; set; }
    public int IdGebouw { get; set; }
    public int Temperatuur { get; set; }
    public string Actie { get; set; }
    public string ProductNaam { get; set; }
    public DateTime AfgenomenTijd { get; set; }
}

I think the mapping between Meting and MetingenView is not configured in AutoMapper. If you use Asp.Net Core, you could create a profile.

public class MetingProfile : Profile
{
    public MetingProfile()
    {
        CreateMap<MetingenView, Meting>();
    }
}

This would create a default mapping that two types have the same property. If you want to config property mapping manually, Function ForMember() would be used.

For example, if you wish that the property MetingenView.IdGebouw maps Meting.IndVerwijderd , you can code this:

CreateMap<MetingenView, Meting>()
    .ForMember(dest=>dest.IdGebouw, opt=>opt.MapFrom(src=>src.IndVerwijderd));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM