Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async Task Main()
- {
- // https://stackoverflow.com/a/65183485/1777780
- // override TypeMapProvider to return custom map for every requested type
- Dapper.SqlMapper.TypeMapProvider = type =>
- {
- var fallback = new DefaultTypeMap(type);
- return new CustomPropertyTypeMap(type, (t, column) =>
- {
- var property = t.GetProperties().FirstOrDefault(prop =>
- prop.GetCustomAttributes(typeof(ColumnAttribute))
- .Cast<ColumnAttribute>()
- .Any(attr => attr.Name == column));
- // if no property matched - fall back to default type map
- if (property == null)
- {
- property = fallback.GetMember(column)?.Property;
- }
- return property;
- });
- };
- using var connection = new SqliteConnection("Data Source=:memory:");
- var result = await connection.QuerySingleAsync<QueryResult>("SELECT 1 AS 'Hello World', 2 AS 'HelloMars'");
- result.Dump();
- }
- class QueryResult
- {
- public int HelloMars { get; set; }
- [Column("Hello World")]
- public int X { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement