Type-handler registered at runtime
SqlMapper.AddTypeHandler mutates a process-global registry when it executes. The generator
runs at compile time, so it cannot see that call - and generated code therefore binds the value
the way it would have without any handler at all: usually a raw p.Value = ..., which the
provider rejects (“No mapping exists from object type …”), or a read that never reaches your
Parse.
SqlMapper.AddTypeHandler(new LocalDateHandler()); // DAP053
The fix: declare the handler instead, so the generator can bake the dispatch:
[module: TypeHandler(typeof(LocalDate), typeof(LocalDateHandler))]
The declarative form is per-assembly rather than process-global, is deterministic (no
startup-ordering races), and shows up in review. An existing handler written against vanilla
Dapper (SqlMapper.TypeHandler<T> / SqlMapper.ITypeHandler) can be named as-is: generated
code adapts it to Dapper.AOT’s IDbValueHandler<T>.
A handler written for Dapper.AOT directly implements IDbValueHandler<T>, usually by
inheriting DbValueHandler<T>:
public sealed class LocalDateHandler : DbValueHandler<LocalDate>
{
protected override void Configure(DbParameter parameter) => parameter.DbType = DbType.Date;
protected override void SetValueCore(DbParameter parameter, LocalDate value)
=> parameter.Value = new DateTime(value.Year, value.Month, value.Day);
protected override LocalDate Parse(object? value) { /* ... */ }
}
Note the limit of this diagnostic: it only sees AddTypeHandler calls in the compilation being
built. A registration performed by a referenced library, or through a helper that hides the
call, cannot be reported - so its absence is not proof that generated code sees every handler.