Reputation:
I am naming my MYSQL tables and columns using underscore characters:
this_is_a_table
should map to: ThisIsATable
this_is_a_column
should map to: ThisIsAColumn
Dapper can handle this mapping if i set:
DefaultTypeMap.MatchNamesWithUnderscores = true;
Is there any way to enable this in Dapper-Extensions so that it maps undescore automatically?
Upvotes: 6
Views: 4366
Reputation: 10209
It's pretty straightforward, you just need to create a custom mapping. Here is an example:
Create table:
create table hello_world
(
Id int not null,
Value_Column varchar(max)
)
Test:
public class World
{
public int Id { get; set; }
public string Value { get; set; }
}
public class WorldCustomMapper : ClassMapper<World>
{
public WorldCustomMapper()
{
base.Table("hello_world");
Map(f => f.Id).Column("Id");
Map(f => f.Value).Column("Value_Column");
}
}
[TestFixture]
public class Class1
{
[Test]
public void TestMappping()
{
var conn = new SqlConnection(@"Data Source=.\sqlexpress; Integrated Security=true; Initial Catalog=mydb");
conn.Open();
var record = new World
{
Id = 1,
Value = "Hi"
};
conn.Insert(record);
var result = conn.Get<World>(1);
Assert.That(result.Value, Is.EqualTo("Hi"));
}
}
Upvotes: 3