title: Dapper with Attributes mapping
body: <p>I try to map my Id fields with the Column Attributes but for some reason this doesn't seem to work and I can't figure out why. I set up a test project to demonstrate what I am trying.</p>
  
  <p>First, I got my 2 entities:</p>
  
  <p>Entity Table1</p>
  
  <pre><code>using System.Data.Linq.Mapping;
  
  namespace DapperTestProj
  {
      public class Table1
      {
          [Column(Name = "Table1Id")]
          public int Id { get; set; }
  
          public string Column1 { get; set; }
  
          public string Column2 { get; set; }
  
          public Table2 Table2 { get; set; }
  
          public Table1()
          {
              Table2 = new Table2();
          }
      }
  }
  </code></pre>
  
  <p>and entity Table2</p>
  
  <pre><code>using System.Data.Linq.Mapping;
  
  namespace DapperTestProj
  {
      public class Table2
      {
          [Column(Name = "Table2Id")]
          public int Id { get; set; }
  
          public string Column3 { get; set; }
  
          public string Column4 { get; set; }
      }
  }
  </code></pre>
  
  <p>In my database I got 2 tables, also named Table1 and Table2. Both tables got their columns named equal to the entities with the exception that Table1 has a column named Table2Id and there is also a foreign key between Table1.Table2Id and Table2.Id. </p>
  
  <p>Also there is 1 record each in both tables and those got both the Id 2.</p>
  
  <p>What I try next is to execute a query with dapper and it should return a object of type Table1. This works, but both the property Table1.Id and Table1.Table2.Id remains 0 (default integer). I expect the column attributes would map the Id fields but clearly this isn't happing.</p>
  
  <p>This is the query and mapping I am executing in code:</p>
  
  <pre><code>private Table1 TestMethod(IDbConnection connection)
  {
      var result = connection.Query&lt;Table1, Table2, Table1&gt;(
          @"SELECT 
               T1.Id as Table1Id, 
               T1.Column1 as Column1,
               T1.Column2 as Column2,
               T2.Id as Table2Id,
               T2.Column3 as Column3,
               T2.Column4 as Column4
            FROM Table1 T1 
            INNER JOIN Table2 T2 ON T1.Table2Id = T2.Id",
          (table1, table2) =&gt;
              {
                  table1.Table2 = table2;
                  return table1;
              },
          splitOn: "Table2Id"
          ).SingleOrDefault();
  
      return result;
  }
  </code></pre>
  
  <p>Now I could rename the both Id property fields in the entities to Table1Id and Table2Id but I prefer Id instead cause of the more logic code like Table1.Id instead of Table1.Table1Id. So I was wondering, is it possible what I want here and if so, how?</p>
  
  <h2>Edit:</h2>
  
  <p>I found this topic: 
  <a href="https://stackoverflow.com/questions/8902674/manually-map-column-names-with-class-properties">Manually Map column names with class properties</a></p>
  
  <p>And with the code in the first post of Kaleb Pederson it is possible to use attributes when needed with the FallBackTypeMapper class and the ColumnAttributeTypeMapper class. All that is needed is to add the required classes to the typemapping with:</p>
  
  <pre><code>SqlMapper.SetTypeMap(typeof(Table1), new ColumnAttributeTypeMapper&lt;Table1&gt;());
  SqlMapper.SetTypeMap(typeof(Table2), new ColumnAttributeTypeMapper&lt;Table2&gt;());
  </code></pre>
  
  <p>But with many entities this list will grow long. Also you need to add every class manually to the list and I was wondering if this could be done automaticly en more generic with Reflection. I found a code fragment that is able to get all the types:</p>
  
  <pre><code>        const string @namespace = "DapperTestProj.Entities";
  
          var types = from type in Assembly.GetExecutingAssembly().GetTypes()
                      where type.IsClass &amp;&amp; type.Namespace == @namespace
                      select type;
  </code></pre>
  
  <p>And looping through all the types, I can do this, only problem I have now is what code fragment do I need to have or need to put on the place where the questionmarks are right now?</p>
  
  <pre><code>        typeList.ToList().ForEach(type =&gt; SqlMapper.SetTypeMap(type, 
                                 new ColumnAttributeTypeMapper&lt;/*???*/&gt;()));
  </code></pre>
  
  <p>Edit:</p>
  
  <p>After more searching, I found the solution for my last problem:</p>
  
  <pre><code>        typeList.ToList().ForEach(type =&gt;
              {
                  var mapper = (SqlMapper.ITypeMap)Activator.CreateInstance(
                      typeof(ColumnAttributeTypeMapper&lt;&gt;)
                          .MakeGenericType(type));
                  SqlMapper.SetTypeMap(type, mapper);
              });
  </code></pre>