Guest User

Untitled

a guest
Feb 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Dapper;
  4. using Microsoft.Extensions.Configuration;
  5.  
  6. namespace Foo.Bar
  7. {
  8. public static class DapperMappers
  9. {
  10. public static void Init(IConfiguration configuration)
  11. {
  12. var mappings = configuration.GetSection("ModelMappings");
  13. foreach (var typeSection in mappings.GetChildren())
  14. {
  15. var columnNames = typeSection.Get<IDictionary<string, string>>();
  16. var mapType = Type.GetType(typeSection.Key);
  17.  
  18. if (mapType == null)
  19. continue;
  20.  
  21. SqlMapper.SetTypeMap(mapType, new CustomPropertyTypeMap(
  22. mapType,
  23. (type, columnName) =>
  24. type.GetProperty(columnNames.ContainsKey(columnName) ? columnNames[columnName] : columnName)));
  25. }
  26. }
  27. }
  28. }
Add Comment
Please, Sign In to add comment