Come fare in modo che AutoMapper tronchi le stringhe in base all'attributo MaxLength?

Come fare in modo che AutoMapper tronchi le stringhe in base all'attributo MaxLength?

Non sono sicuro che sia un buon posto per mettere quella logica, ma ecco un esempio che dovrebbe funzionare nel tuo caso (AutoMapper 4.x):Mappatura personalizzata con AutoMapper

In questo esempio, sto leggendo un MapTo personalizzato proprietà sulla mia entità, potresti fare lo stesso con MaxLength .

Ecco un esempio completo con la versione corrente di AutoMapper (6.x)

class Program
{
    static void Main(string[] args)
    {
        Mapper.Initialize(configuration =>
            configuration.CreateMap<Dto, Entity>()
                .ForMember(x => x.Name, e => e.ResolveUsing((dto, entity, value, context) =>
                {
                    var result = entity.GetType().GetProperty(nameof(Entity.Name)).GetCustomAttribute<MaxLengthAttribute>();
                    return dto.MyName.Substring(0, result.Length);
                })));

        var myDto = new Dto { MyName = "asadasdfasfdaasfasdfaasfasfd12" };
        var myEntity = Mapper.Map<Dto, Entity>(myDto);
    }
}

public class Entity
{
    [MaxLength(10)]
    public string Name { get; set; }
}

public class Dto
{
    public string MyName { get; set; }
}