C
C#17mo ago
Johannes M

✅ AutoMapper - mapping record to a class (.NET 7)

I am using mediatr for my project and I have my command using a sealed record and when I try to map my record to my entity I run into an AutoMapper Extension error. Error: Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll CODE:
public sealed record CreateProductCommand(string Name, string? Description, string Code, decimal Price);

public class Product : BaseAuditableEntity
{
public required string Name { get; set; }
public string? Code { get; set; }
public string? Description { get; set; }
public decimal Price { get; set; }
}

public async Task<Result> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
var product = _mapper.Map<Product>(request);
product.AddDomainEvent(new CreatedEvent<Product>(product));
_unitOfWork.Products.Insert(product);
await _unitOfWork.SaveChangesAsync(cancellationToken);

return Result.Success();
}
public sealed record CreateProductCommand(string Name, string? Description, string Code, decimal Price);

public class Product : BaseAuditableEntity
{
public required string Name { get; set; }
public string? Code { get; set; }
public string? Description { get; set; }
public decimal Price { get; set; }
}

public async Task<Result> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
var product = _mapper.Map<Product>(request);
product.AddDomainEvent(new CreatedEvent<Product>(product));
_unitOfWork.Products.Insert(product);
await _unitOfWork.SaveChangesAsync(cancellationToken);

return Result.Success();
}
When my CreateProductCommand was still a class the mapping worked with no issues.
10 Replies
HtmlCompiler
HtmlCompiler17mo ago
aren't there more details in the exception?
Johannes M
Johannes MOP17mo ago
Unfortunately not. Just that and that's all. But I was able to narrow it down to that it's an issue when I try map a record with a class.
HtmlCompiler
HtmlCompiler17mo ago
i guess you ould try explicit construction of the record from automapper? even if just as a test
blueberriesiftheywerecats
the problem is that record wants to get propertys through constructor automapper cant do that by default
HtmlCompiler
HtmlCompiler17mo ago
you can try ConstructUsing or the other one
blueberriesiftheywerecats
Stack Overflow
AutoMapper: Problem with mapping records type
I am mapping with automapper 10.1.1 in c# 9 from this class public record BFrom { public Guid Id { get; init; } public Guid DbExtraId { get; init; } } into this public record ATo(Guid Id,...
HtmlCompiler
HtmlCompiler17mo ago
like ForCtorSomething
Johannes M
Johannes MOP17mo ago
Would I then have to create a map profile for each and every mapping I will do? Is there a way I can just make it generic to automatically map the properties?
HtmlCompiler
HtmlCompiler17mo ago
if it doesn't work without ForCtorParam that's what you have to do i've never searched if there is some source generator or whatever that reduces this effort
Johannes M
Johannes MOP17mo ago
Shucks that is gonna be a tedious effort for each and everytime a new record is added and a particular mapping is required.

Did you find this page helpful?