Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Dapper;
- using Microsoft.Extensions.Configuration;
- namespace Foo.Bar
- {
- public static class DapperMappers
- {
- public static void Init(IConfiguration configuration)
- {
- var mappings = configuration.GetSection("ModelMappings");
- foreach (var typeSection in mappings.GetChildren())
- {
- var columnNames = typeSection.Get<IDictionary<string, string>>();
- var mapType = Type.GetType(typeSection.Key);
- if (mapType == null)
- continue;
- SqlMapper.SetTypeMap(mapType, new CustomPropertyTypeMap(
- mapType,
- (type, columnName) =>
- type.GetProperty(columnNames.ContainsKey(columnName) ? columnNames[columnName] : columnName)));
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment