Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
Created September 22, 2020 15:25
Show Gist options
  • Save smailliwcs/e2a3c389fbd5baf9d46d6ae8b09f5ad5 to your computer and use it in GitHub Desktop.
Save smailliwcs/e2a3c389fbd5baf9d46d6ae8b09f5ad5 to your computer and use it in GitHub Desktop.
Mapping qualified column names in Dapper
using Dapper;
using System;
using System.Reflection;
public class QualifiedTypeMap : SqlMapper.ITypeMap
{
private static string Unqualify(string columnName)
{
return columnName.Substring(columnName.LastIndexOf('.') + 1);
}
private DefaultTypeMap @base;
public QualifiedTypeMap(Type type)
{
@base = new DefaultTypeMap(type);
}
public ConstructorInfo FindConstructor(string[] names, Type[] types)
{
return @base.FindConstructor(names, types);
}
public ConstructorInfo FindExplicitConstructor()
{
return @base.FindExplicitConstructor();
}
public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
{
return @base.GetConstructorParameter(constructor, Unqualify(columnName));
}
public SqlMapper.IMemberMap GetMember(string columnName)
{
return @base.GetMember(Unqualify(columnName));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment