Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace YourNamespace
{
/// <summary>
/// Uses the Name value of the <see cref="ColumnAttribute"/> specified to determine
/// the association between the name of the column in the query results and the member to
/// which it will be extracted. If no column mapping is present all members are mapped as
/// usual.
/// </summary>
/// <typeparam name="T">The type of the object that this association between the mapper applies to.</typeparam>
public class ColumnAttributeTypeMapper<T> : FallbackTypeMapper
{
public ColumnAttributeTypeMapper()
: base(new SqlMapper.ITypeMap[]
{
new CustomPropertyTypeMap(
typeof(T),
(type, columnName) =>
type.GetProperties().FirstOrDefault(prop =>
prop.GetCustomAttributes(false)
.OfType<ColumnAttribute>()
.Any(attr => attr.Name == columnName)
)
),
new DefaultTypeMap(typeof(T))
})
{
}
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class ColumnAttribute : Attribute
{
public string Name { get; set; }
}
public class FallbackTypeMapper : SqlMapper.ITypeMap
{
private readonly IEnumerable<SqlMapper.ITypeMap> _mappers;
public FallbackTypeMapper(IEnumerable<SqlMapper.ITypeMap> mappers)
{
_mappers = mappers;
}
public ConstructorInfo FindConstructor(string[] names, Type[] types)
{
foreach (var mapper in _mappers)
{
try
{
ConstructorInfo result = mapper.FindConstructor(names, types);
if (result != null)
{
return result;
}
}
catch (NotImplementedException)
{
}
}
return null;
}
public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
{
foreach (var mapper in _mappers)
{
try
{
var result = mapper.GetConstructorParameter(constructor, columnName);
if (result != null)
{
return result;
}
}
catch (NotImplementedException)
{
}
}
return null;
}
public SqlMapper.IMemberMap GetMember(string columnName)
{
foreach (var mapper in _mappers)
{
try
{
var result = mapper.GetMember(columnName);
if (result != null)
{
return result;
}
}
catch (NotImplementedException)
{
}
}
return null;
}
public ConstructorInfo FindExplicitConstructor()
{
return _mappers
.Select(mapper => mapper.FindExplicitConstructor())
.FirstOrDefault(result => result != null);
}
}
}
@ronbuchanan

This comment has been minimized.

Copy link

@ronbuchanan ronbuchanan commented Nov 7, 2013

This didn't work for me. In the end I had to put the column names in double-quotes (") matching the properties of the object I wanted mapped. Not sure if I was using the wrong ColumnAttribute class (System.ComponentModel.DataAnnotations.Schema) or not.

@Crisfole

This comment has been minimized.

Copy link

@Crisfole Crisfole commented Jun 25, 2014

@ronbuchanan The attribute is in the above code. Look for "ColumnMappingAttribute". It's a custom, very simple, one.

@LosManos

This comment has been minimized.

Copy link

@LosManos LosManos commented Apr 10, 2015

Example of class is

public class Customer
{
    public int CustomerID { get; set; }

    [Column(Name = "Customer_Name")]
    public string Name{ get; set; }
}
@jessiepinkman

This comment has been minimized.

Copy link

@jessiepinkman jessiepinkman commented Jun 11, 2015

Hey Guys,
Did anything change recently on the dapper side in relation to this. Because I am unable to get this thing to work on latest Dapper 1.42.

I added the above class into my code and then added the below attribute..
Also, I added below two lines in my main function.

 var mapper = (SqlMapper.ITypeMap)Activator
      .CreateInstance(typeof(ColumnAttributeTypeMapper<>)
                      .MakeGenericType(typeof(Customer)));
  SqlMapper.SetTypeMap(typeof(Customer), mapper);

.And when I call the stored procedure which returns the data, I am not seeing the first attribute being populated. Any help would be really appreciated

[Column(Name = "Customer_NM")]
public string CustomerName{ get; set; }

public string Phone{ get; set; }
@ITCBB

This comment has been minimized.

Copy link

@ITCBB ITCBB commented Aug 23, 2017

Trying to use it with the current version, this is missing the ITypeMap method FindExplicitConstructor

@RhodWulph

This comment has been minimized.

Copy link

@RhodWulph RhodWulph commented Jan 24, 2018

This is working for me in Dapper 1.50.2, but you have to implement the method FindExplicitConstructor like this, @ITCBB:

    {
        foreach (var mapper in _mappers)
        {
            try
            {
                ConstructorInfo result = mapper.FindExplicitConstructor();
                if (result != null)
                {
                    return result;
                }
            }
            catch (NotImplementedException)
            {
            }
        }
        return null;
    }
@washingtonpraxedes

This comment has been minimized.

Copy link

@washingtonpraxedes washingtonpraxedes commented Mar 28, 2018

Does this map work for PK attributes ?

@tingwei628

This comment has been minimized.

Copy link

@tingwei628 tingwei628 commented Jan 16, 2020

It worked in Dapper 1.60.6.

NOTE : If you want to map properties of the model which has already Column attribute in Entity Framework, you don't need to define another custom ColumnAttribute

Just replace ColumnAttribute with System.ComponentModel.DataAnnotations.Schema.ColumnAttribute, like this

new CustomPropertyTypeMap(
                       typeof(T),
                       (type, columnName) =>
                           type.GetProperties().FirstOrDefault(prop =>
                               prop.GetCustomAttributes(false)
                                   .OfType<System.ComponentModel.DataAnnotations.Schema.ColumnAttribute>()
                                   .Any(attr => attr.Name == columnName)
                               )
                       ),
@auroraegan

This comment has been minimized.

Copy link

@auroraegan auroraegan commented Mar 29, 2020

Sorry, currently, I read and try to understand for use this library. But, I don't know how to apply it in .net core 2.1?

` public async Task<TEntity> RawQuerySingleAsync(string query)
        {
            using (var connection = new SqlConnection(ServerConnectionString))
            {
                      var k = new ColumnAttributeTypeMapper<TEntity>(); //????
                var result = await connection.QueryFirstOrDefaultAsync<TEntity>(query);
          
                
                return result;
            }
        }`

my simple has: select * from Users and have Column User_Id, It's must bi mapped with UserId?

currently, my dapper version: 2.0.30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment